简体   繁体   English

如何在onPause和onResume上对其静音和取消静音

[英]How to mute and unmute it on the onPause and onResume

I'm having a problem when I set the audio to Mute on the onPause() and in the onResume() I try to unmute it but without success. 当我在onPause()上将音频设置为“静音”时,在onResume()尝试将其取消静音但没有成功时,我遇到了问题。

Code: 码:

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

Any help is welcome, thanks. 欢迎任何帮助,谢谢。

MediaPlayer ourSong;
int length; // To save the last seconds where the music has paused

In your OnCreate 在您的OnCreate中

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

In you pause function 在您暂停功能

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

In you resume function 在你恢复功能

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

Note: 注意:

you should make a folder row and then YourSong has to be like this R.raw.yoursong 您应该制作一个文件夹row ,然后YourSong必须像这样R.raw.yoursong

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM