Jak wyciszyć MediaPlayer w Androidzie

Jak wyciszyć MediaPlayer w Androidzie

Author: anticafe, 2011-01-01

6 answers

Ten kod zadziałał dla mnie,

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

Dla Niemy

mp.setVolume(0,0);

& Unmute lub full volume

mp.setVolume(1,1);
 51
Author: Parag Chauhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-26 11:21:26
 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);
 12
Author: Ramesh Akula,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-03 12:12:04

Możesz chcieć wyciszyć / wyciszyć dźwięk, zamiast natychmiast ustawić go na pełną (1.0 f) lub zerową (0.0 f) głośność.

Jest to trochę trudne, ponieważ potrzebujesz zadania asynchronicznego, aby Twoja aplikacja była responsywna:

MpMain jest moją instancją MediaPlayer do odtwarzania zapętlonej muzyki w tle. Zależy mi również na OnPause () i OnResume ().

Metoda playBackgroundMusic służy do włączania muzyki, fadeOutBackgroundMusic wyłącza muzykę, ale przez zanik głośności na 0,5 sekundy (10*50, patrz while I wątek.snu).

Mój base-volume to 0.8 f, możesz zrobić z tego parametr zadania asynchronicznego lub użyć statycznej zmiennej globalnej.

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;
    }
  }
}
 8
Author: Michael Biermann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-25 22:55:26

AudioManger.setStreamMute jest przestarzały. użyj poniższego kodu alternatywnego.

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);
    }
 2
Author: Naveen Kumar M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-13 11:44:37

Spróbuj tego...

   AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
[1]}wyciszy pełną głośność mediów, aby wyciszyć
 1
Author: sandy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-17 07:29:17

Spróbuj tego

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

 1
Author: Uma sankar pradhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-13 00:51:46