开发者

How to mute MediaPlayer in android

How to mute MediaPl开发者_运维问答ayer in android


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


 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);
    }


We can use below methods to use for mute/unmute or to check whether the audio player is in mute state or not

/**
 * To Mute or Un-mute using Audio Manager
 */
private fun updateMuteState(mute: Boolean) {
    activity?.let {
        getAudioManager(it).adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            if (mute) -100 else 100,
            0
        )
    }
}

/**
 * Return true -> audio player is in mute state, false otherwise
 */
private fun isMute(): Boolean {
    activity?.let {
        getAudioManager(it).getStreamVolume(AudioManager.STREAM_MUSIC).let { volume ->
            return volume == 0
        }
    }
    return false
}

/**
 * To Get the Audio Manager
 */
private fun getAudioManager(context: Context) =
    context.getSystemService(Context.AUDIO_SERVICE) as AudioManager


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


What worked for me for un-mute even in silent mode

 MediaPlayer mMediaPlayer = new MediaPlayer();
 mMediaPlayer.setDataSource(this, alarmUri);
 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
 mMediaPlayer.setVolume(0.7f,0.7f);
 mMediaPlayer.prepare();
 mMediaPlayer.start();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜