How do I mute all sounds of my application? - android

I'm creating a quiz app just for learning, and I've got a problem.
In my mainactivity I have a media player. It plays music even going through other activities, and I have a mute button to stop the music. I also have a sound effect when I successfully hit the right answer (on a question which is in other activity). But I don't know how to mute this SFX along with the background music when I press the mute button. Any hints? ;)
If it has a way to mute my entire application will be so good. I found some ways to mute, but this way will mute the entire system. And it's
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

If you want to mute only the running apps sound, then you better do this:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); //Use your mediaplayer instance instead of this. Also maintain a single instance of mediaplayer for the entire app.
mp.start();
For Mute
mp.setvolume(0,0);
& Unmute or full volume
mp.setvolume(0,1);
Reference:
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)

You can just use AudioManager.setStreamMute(). Feel free to use the code below.
//mute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//unmute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);

Try this. It may be of help to you.
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

Related

How can I use the androids' music stream as the data source of the media player library

In my app, I am trying to alter the volume of both(left/right headphone music streams) coming out of the device with a Seekbar.
The AudioManager Class can access the music stream coming from the device:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxValue = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curValue = am.getStreamVolume(AudioManager.STREAM_MUSIC);
But the AudioManager .setStreamVolume() class can only change the volume coming from both ears together.
I figured out how to set the volume separately with the MediaPlayer class but how do I link the MediaPlayer class's .setDataSource() method to the stream coming out of the android device?
I looked everywhere and still haven't found an answer. Any help is appreciated!
You use setAudioStreamType() after you setDataSource(), but before calling
prepare

Playing voicemail via MediaPlayer turns alarm volume silent

I play an audio file from a URL using MediaPlayer. The file plays fine, however after playback (if the app hasnt't been explicitly killed), alarm ringtone is silent. What do I need to clear/reset on end of playback to make this not happen?
Code for the file I am using to control the voicemail playback.
http://hastebin.com/ehijobuwoc.coffee
You can try getStreamVolume at start of playback then use setStreamVolume to set it back after your playback has finished.
http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
Ok have found something that works. Solves both the alarm issue and allows me to play the voicemail through the earpiece.
I needed to setMode back to what it was when I started, in onStop():
// set mode back to normal, and speakerphone back to true.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(prevMode);
am.setSpeakerphoneOn(true);
and in init:
private void initMediaPlayer() {
// Make the media play through earpiece instead of through speaker(default)
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
prevMode = am.getMode();
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(false);
Thanks #DreadfulWeather for leading me to this answer, with some helpful questions.

How do I get the system volume programatically

I want to get the system volume level to use programatically, the one you set with the hardware keys when being outside any application.
I tried with below method but it doesnt do anything
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.notif);
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int volume_level = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
mediaPlayer.setAudioStreamType(volume_level);
mediaPlayer.start();
What do I need to change?
You have to change this line
mediaPlayer.setAudioStreamType(volume_level);
to this:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
setAudioStreamType()
Used to identify the type of audio stream
AudioManager.STREAM_ALARM is for alarm
AudioManager.STREAM_DTMF is for touch sounds
AudioManager.STREAM_MUSIC is for music
AudioManager.STREAM_NOTIFICATION is for notification
AudioManager.STREAM_RING is for phone ring
AudioManager.STREAM_SYSTEM is for system sounds
AudioManager.STREAM_VOICE_CALL is for phone calls
Please reffer offical doc here to set or get the system volume of android device.
To set volume level to 20 you can use following code:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

Focus on background Music when in a call

I am developing an app. I need to play the music when i am in a call. But i am facing with the problem that whenever i try to play the music in a call it is not that much loud as it is when there is no call.
Is there any way to control the background music when in a call. My music volume becomes softer when i am in a call but it becomes louder as soon as i end the call.
final AudioManager mAudioManager = (AudioManager) ctx
.getSystemService(AUDIO_SERVICE);
final int originalVolume = mAudioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager
.setStreamVolume(
AudioManager.STREAM_MUSIC,
mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
Class audioSystemClass = Class
.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod(
"setForceUse", int.class, int.class);
// First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go
// back to the default
// behavior, use FORCE_NONE (0).
setForceUse.invoke(null, 1, 1);
this is my code.
I see that you're trying to use the "force music streams to the loudspeaker"-method I posted in an answer to another question.
The problem is that during voice calls, music streams will follow the voice call's routing and ignore the FOR_MEDIA force flag. So what you perceive as lower volume is most likely the result of the music being played from the earpiece instead of from the loudspeaker.
To my knowledge, there's no way of routing the music to the loudspeaker during an ongoing call without also routing the voice call to the loudspeaker (i.e. using setSpeakerPhoneOn).
Depending on how your phone manufacturer/carrier implemented Android AudioPolicyService, some implementation lowers STREAM_MUSIC volume when MODE_IN_CALL/MODE_IN_COMMUNICATION is in session.
I have not tried this, but a likely workable solution is to use STREAM_VOICE_CALL instead of STREAM_MUSIC. Your audio stream should then play along with the in call audio stream just fine.

Android knowing if music is playing in specific instance of MediaPlayer

I'd like to know if my Specific MediaPlayer instance is playing sound/music.
MediaPlayer has the isPlaying method but it returns true even if the player is buffering and not playing.
Any idea how to know if MediaPlayer really plays sound?
Thanks.
You can check the state of AudioPlayer by isMusicActive() method by respective method.
The code snippet is as below :
AudioManager ar = (AudioManager) getSystemService(AUDIO_SERVICE);
if(ar.isMusicActive()) {
//used
}

Categories

Resources