How To Mute Mediaplayer In Android
Solution 1:
This code worked for me,
MediaPlayermp= MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
for Mute
mp.setVolume(0,0);
& Unmute or full volume
mp.setVolume(1,1);
Solution 2:
AudioManagermAudioManager= (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
intcurrent_volume=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//If you want to player is mute ,then set_volume variable is zero.Otherwise you may supply some value.int set_volume=0;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,set_volume, 0);
Solution 3:
You might want to fade out/in the sound instead of setting it immediately to full (1.0f) or zero (0.0f) volume.
It's a little bit tricky, because you need an async task to keep your app responsive:
mpMain is my MediaPlayer instance to play the looping background music. I also care about it in OnPause() and OnResume().
Method playBackgroundMusic is used to turn the music on, fadeOutBackgroundMusic turns music off, but by fading out volume for 0.5 seconds (10*50, see while and Thread.sleep).
My base-volume is 0.8f, you might want to make that a parameter of the async task or use a static global variable.
publicvoidplayBackgroundMusic(Boolean isOn ){
if(isOn){
// prevent conflics with async fade-out taskif(mtask_fadeout_music!=null){
mtask_fadeout_music.cancel(true);
mtask_fadeout_music=null;
}
if(mpMain==null){
mpMain = MediaPlayer.create(this, R.raw.zin___piano_2_140bpm_32158);
mpMain.setLooping(true);
mpMain.setVolume(0.8f,0.8f);
mpMain.start();
}
}
else{
if(mtask_fadeout_music==null){
fadeOutBackgroundMusic();
}
}
}
publicvoidfadeOutBackgroundMusic(){
mtask_fadeout_music = (FadeOutMusic)newFadeOutMusic( ).execute( );
}
//// background processing ... fade out music stream//publicclassFadeOutMusicextendsAsyncTask<String,Integer,String> {
@OverrideprotectedStringdoInBackground(String... args) {
float level = 0.8f;
int i = 1;
while(i<50){
i++;
if(mpMain != null){
level=level*0.9f;
mpMain.setVolume(level, level);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return"dummy";
}
@OverrideprotectedvoidonPostExecute(String dummy ) {
if(mpMain != null){
mpMain.setVolume(0,0);
mpMain.release();
mpMain = null;
}
if(mtask_fadeout_music!=null){
mtask_fadeout_music = null;
}
}
@OverridepublicvoidonCancelled() {
if(mpMain != null){
mpMain.setVolume(0,0);
mpMain.release();
mpMain = null;
}
if(mtask_fadeout_music!=null){
mtask_fadeout_music = null;
}
}
}
Solution 4:
AudioManger.setStreamMute
is deprecated.
use below alternative code.
AudioManageram= getSystemService(Context.AUDIO_SERVICE);
// Change the stream to your stream of choice. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
Solution 5:
Try this...
AudioManagermanager= (AudioManager) getSystemService(Context.AUDIO_SERVICE);
manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
It will mute Complete media volume to mute
Post a Comment for "How To Mute Mediaplayer In Android"