Question

How to mute MediaPlayer in android

Was it helpful?

Solution

This code worked for me,

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

for Mute

mp.setVolume(0,0);

& Unmute or full volume

mp.setVolume(1,1);

OTHER TIPS

 AudioManager   mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

    int current_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);

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.

public void playBackgroundMusic( Boolean isOn ){
  if(isOn){
    // prevent conflics with async fade-out task
    if(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();
    }
  }
}

public void fadeOutBackgroundMusic(){
  mtask_fadeout_music = (FadeOutMusic)new FadeOutMusic( ).execute( );
}


//
//  background processing ... fade out music stream
//
public class FadeOutMusic extends AsyncTask<String,Integer,String> {

  @Override
  protected String doInBackground(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";
  }

  @Override
  protected void onPostExecute( String dummy ) {
    if(mpMain != null){
      mpMain.setVolume(0,0);         
      mpMain.release();
      mpMain = null;  
    }
    if(mtask_fadeout_music!=null){
      mtask_fadeout_music = null;
    }
  }       

  @Override
  public void onCancelled() {
    if(mpMain != null){
      mpMain.setVolume(0,0);         
      mpMain.release();
      mpMain = null;  
    }
    if(mtask_fadeout_music!=null){
      mtask_fadeout_music = null;
    }
  }
}

AudioManger.setStreamMute is deprecated. use below alternative code.

AudioManager am = 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);
    }

Try this...

   AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
manager.setStreamMute(AudioManager.STREAM_MUSIC, true);

It will mute Complete media volume to mute

Try this

AudioManager am = getSystemService(Context.AUDIO_SERVICE);
// Request audio focus.
am.requestAudioFocus(listener, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);

http://developer.android.com/training/managing-audio/audio-focus.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top