gpt4 book ai didi

android - 状态改变时需要关闭MediaRecorder播放的声音

转载 作者:行者123 更新时间:2023-11-29 00:32:52 25 4
gpt4 key购买 nike

我已经尝试了在下面的链接中找到的更改,但无济于事。

How to shut off the sound MediaRecorder plays when the state changes

我都试过了

AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC,false);

// disable sound for recording.   
// disable sound when recording.
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_ALARM,true);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_DTMF,true);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_MUSIC,true);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_RING,true);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_SYSTEM,true);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_VOICE_CALL,true);

但是当我启动媒体记录器时,我仍然能听到声音。我尝试将此代码放在应用程序的开头和紧接在:

mediaRecorder.start();

还有其他建议吗?

最佳答案

SetStreamMute 仅适用于 SYSTEM 和 MUSIC。对于所有其他你应该使用 SetStreamVolume。

试试下面的代码:

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC,true);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
audioManager.setStreamVolume(AudioManager.STREAM_DTMF, 0, 0);
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);
audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);

完整性检查:

Log.d(TAG, String.format("%d,  %d, %d, %d, %d, %d, %d",
audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM),
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC),
audioManager.getStreamVolume(AudioManager.STREAM_ALARM),
audioManager.getStreamVolume(AudioManager.STREAM_DTMF),
audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION),
audioManager.getStreamVolume(AudioManager.STREAM_RING)
));

结果应为:0, 0, 0, 0, 0, 0

请记住,带有音量的游戏(不包括 setStreamMute)不会在 Activity 结束后重置之前的值,因此您可能希望将它们存储起来以在 onDestroy() 或更早的时间恢复。

关于android - 状态改变时需要关闭MediaRecorder播放的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14369445/

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