【问题标题】:Android AudioManager, after mute can NOT unmute itAndroid AudioManager,静音后无法取消静音
【发布时间】:2015-04-10 06:54:32
【问题描述】:

我在我的应用中构建了一个简单的视频播放器。

我只想通过耳机而不是扬声器播放音频。

所以,我做了以下

onCreate

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

....

@SuppressWarnings("deprecation")
private void checkJack() {
    if(Constants.HEADSET_ONLY && !musicReceiverStarted) {
        Log.v(TAG + " | AudioManager checkJack", "Running checkJack()");
        audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        try 
         {

            if(!audio.isWiredHeadsetOn()) {
                Log.v(TAG + " | AudioManager checkJack", "Headset is unplugged");
                audio.setStreamMute(AudioManager.STREAM_MUSIC, true); // Mute
                Toast.makeText(DuplicatedPlayerActivity.this, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_SHORT).show();
            }
            else {
                Log.v(TAG + " | AudioManager checkJack", "Headset is plugged");
                audio.setStreamMute(AudioManager.STREAM_MUSIC, false); // UnMute
            }
        } 
        catch (Exception e) { e.printStackTrace(); }
    }
}//end checkJack

然后我有一个接收器:

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {

            musicReceiverStarted = true;
            audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            int state = intent.getIntExtra("state", -1);
            switch (state) {

                case 0:
                    Log.d(TAG + " | AudioManager", "Headset is unplugged");
                    audio.setStreamMute(AudioManager.STREAM_MUSIC, true); // Mute
                    Toast.makeText(DuplicatedPlayerActivity.this, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_SHORT).show();
                    break;

                case 1:
                    Log.d(TAG + " | AudioManager", "Headset is plugged");
                    audio.setStreamMute(AudioManager.STREAM_MUSIC, false); // UnMute
                    break;

                default:
                    Log.d(TAG + " | AudioManager", "I have no idea what the headset state is");
                    audio.setStreamMute(AudioManager.STREAM_MUSIC, true); // Mute
                    Toast.makeText(DuplicatedPlayerActivity.this, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_SHORT).show();
            }
        }//end if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG))
    }//end onReceive
}//end MusicIntentReceiver

现在的问题是:两个函数都运行正确。但是在 checkJack() 中的音频已被静音之后;它不会在 MusicIntentReceiver() 中取消静音;

此外,MusicIntentReceiver() 仅在插孔状态发生更改时才会执行。所以我不能第一次用它来静音,因为它永远不会被执行。

任何建议。

【问题讨论】:

  • 您确定您没有将流静音两次(或更多次)吗?您必须将流取消静音的次数与静音次数相同。

标签: java android android-resources android-audiomanager


【解决方案1】:

让您的音频管理器全局化,然后创建一个布尔值以确保您只静音一次。

另见 Mute the global sound in Android

private void muteAudio() {      
    audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audio.setStreamMute(AudioManager.STREAM_NOTIFICATION,   true);
    audio.setStreamMute(AudioManager.STREAM_ALARM,          true);
    audio.setStreamMute(AudioManager.STREAM_MUSIC,          true);
    audio.setStreamMute(AudioManager.STREAM_RING,           true);
    audio.setStreamMute(AudioManager.STREAM_SYSTEM,         true);

    isAudioMuted = true;
}

private void unmuteAudio() {
    audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audio.setStreamMute(AudioManager.STREAM_NOTIFICATION,   false);
    audio.setStreamMute(AudioManager.STREAM_ALARM,          false);
    audio.setStreamMute(AudioManager.STREAM_MUSIC,          false);
    audio.setStreamMute(AudioManager.STREAM_RING,           false);
    audio.setStreamMute(AudioManager.STREAM_SYSTEM,         false);

    isAudioMuted = false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    相关资源
    最近更新 更多