Thiago
Thiago

Reputation: 13302

Android AudioManager, after mute can NOT unmute it

I build a simple video player in my App.

I only want to play the audio via the headset not speakers.

so, I do the following

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

Then I have a receiver :

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

Now the problem is: Both function runs correct. but After the Audio has beeb muted in the checkJack(); It wont unmute in MusicIntentReceiver();

Also the MusicIntentReceiver() only get execute when there is a jack state changed. so I can not use it for mute the audio for the first time, because it will never get execute.

Any advice.

Upvotes: 2

Views: 2909

Answers (1)

Mr T
Mr T

Reputation: 1509

Make you audio manager global, then create a boolean to make sure you only mute once.

also see 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;
}

Upvotes: 3

Related Questions