gpt4 book ai didi

java - Android AudioManager,静音后无法取消静音

转载 作者:搜寻专家 更新时间:2023-11-01 08:44:44 25 4
gpt4 key购买 nike

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

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

所以,我执行以下操作

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() 仅在插孔状态发生变化时才执行。所以我第一次不能用它来静音,因为它永远不会执行。

任何建议。

最佳答案

让你的音频管理器成为全局的,然后创建一个 boolean 值来确保你只静音一次。

另见 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;
}

关于java - Android AudioManager,静音后无法取消静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29555190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com