如何静音和取消静音它的的onPause和的onResume(How to mute and unmu

2019-07-20 00:06发布

我有一个问题,当我设置音频静音上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);
      }

欢迎任何帮助,谢谢。

Answer 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



文章来源: How to mute and unmute it on the onPause and onResume