Android AudioManager,静音后无法取消静音
Android AudioManager, after mute can NOT unmute it
我在我的应用程序中构建了一个简单的视频播放器。
我只想通过耳机而不是扬声器播放音频。
所以,我执行以下操作
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() 仅在插孔状态发生变化时才执行。所以我第一次不能用它来静音,因为它永远不会被执行。
任何建议。
让你的音频管理器全局化,然后创建一个布尔值来确保你只静音一次。
另见
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;
}
我在我的应用程序中构建了一个简单的视频播放器。
我只想通过耳机而不是扬声器播放音频。
所以,我执行以下操作
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() 仅在插孔状态发生变化时才执行。所以我第一次不能用它来静音,因为它永远不会被执行。
任何建议。
让你的音频管理器全局化,然后创建一个布尔值来确保你只静音一次。
另见 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;
}