好的,接受的答案对我不起作用。在通过 MediaRecorder 录制时,在运行 4.2.1 (Jelly Bean) 的 Galaxy Nexus 上,我需要使用 AudioManager.STREAM_RING,因为 AudiManager.STREAM_SYSTEM 不起作用。它总是在每次录音开始时播放“铃声”。
这是我使用“STREAM_RING”等的解决方案。
// 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);
// re-enable sound after recording.
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_ALARM,false);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_DTMF,false);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_MUSIC,false);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_RING,false);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_SYSTEM,false);
((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_VOICE_CALL,false);
正如documentation 所指出的,请务必在onPause() 方法中重新启用音频。
我希望这可以帮助那些为这个问题而烦恼的人。这将禁用所有声音流。您可以查看并确定您特别需要哪些。我发现当 mediarecorder 运行时,不同版本的 android 使用不同的流作为铃声。即,STREAM_RING 适用于 android 4.2.1 但不适用于 ICS。
编辑:正如我在下面的评论中提到的,我无法在三星 Galaxy S1 上禁用 Android OS 2.3.3 的声音。有谁有这方面的运气吗?
Darrenp 的解决方案有助于整理代码,但在最近对我的手机(galaxy nexus android 4.3)的更新中,音量/录音哔声再次响起! user1944526 的解决方案绝对有帮助。为了使它更容易理解...
使用类似 ,
// class variables.
Integer oldStreamVolume;
AudioManager audioMgr;
enableSound() {
setMuteAll(false);
if (audioMgr != null && oldStreamVolume != null) {
audioMgr.setStreamVolume(AudioManager.STREAM_RING, oldStreamVolume, 0);
}
}
disableSound() {
audioMgr = (AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
oldStreamVolume = audioMgr.getStreamVolume(AudioManager.STREAM_RING);
audioMgr.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
}
为了完整起见,您还可以在函数中调用 darrenp 的解决方案或包含上述所有 STREAM_ 行。但现在对我来说,这些结合起来是有效的。希望这对某人有所帮助...