Android AudioManager,静音后无法取消静音 [英] 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.
所以,我执行以下操作
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
现在的问题是:两个函数都运行正常。但音频在checkJack()中被静音之后;它不会在MusicIntentReceiver()中取消静音;
Now the problem is: Both function runs correct. but After the Audio has beeb muted in the checkJack(); It wont unmute in MusicIntentReceiver();
此外,只有当插孔状态发生变化时,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.
任何建议。
推荐答案
让你的音频管理器全局,然后创建一个布尔值,以确保你只静音一次。
Make you audio manager global, then create a boolean to make sure you only mute once.
也看到
将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;
}
这篇关于Android AudioManager,静音后无法取消静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!