【问题标题】:How to mute and unmute it on the onPause and onResume如何在 onPause 和 onResume 上静音和取消静音
【发布时间】:2013-02-15 16:48:34
【问题描述】:

当我在onPause()onResume() 上将音频设置为静音时遇到问题,我尝试取消静音但没有成功。

代码:

    protected void onPause() {
            super.onPause();
            Log.d(TAG, "onPause");
            setStreamMute(true);        
         }

    protected void onResume() {
            super.onResume();
            Log.d(TAG, "onResume");
            setStreamMute(false);
        }

    public void setStreamMute (boolean state){
            Log.d(TAG,"SetMute: "+state);
            myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            myAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, state);
      }

欢迎任何帮助,谢谢。

【问题讨论】:

  • 尝试使用applicationContext 例如getApplicationContext().getSystemService(Context.AUDIO_SERVICE)
  • 同样的问题@iTech 没有取消静音。
  • @A--C 谢谢,但我还是有同样的问题>
  • 静音请求是累积的,因此您必须有相同数量的取消静音请求才能完全取消静音。您的应用程序中是否还有其他地方发送静音请求?

标签: android audio onresume onpause mute


【解决方案1】:
MediaPlayer ourSong;
int length; // To save the last seconds where the music has paused

在你的 OnCreate

ourSong = MediaPlayer.create(yourActivity.this, YourSong);
        ourSong.setLooping(true);
ourSong.start();

在你暂停功能

ourSong.pause();
length = ourSong.getCurrentPosition();

在你恢复功能

ourSong.seekTo(length);
ourSong.start();

注意:

你应该创建一个文件夹row 然后YourSong 必须是这样的R.raw.yoursong

【讨论】:

  • 如果调用onShop(),最好将长度保存在SharedPreferences中。
  • 取决于您的应用程序,但几乎正确
猜你喜欢
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2020-08-31
  • 2011-01-21
  • 1970-01-01
  • 2011-12-09
相关资源
最近更新 更多