当状态改变时,需要关闭MediaRecorder播放的声音

时间:2013-01-16 22:59:00

标签: android cordova audio mute

我已尝试过以下链接中发现的更改,但无济于事。

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();

还有其他建议吗?

1 个答案:

答案 0 :(得分:2)

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)在活动消失后不会重置之前的值,因此您可能希望将它们存储在onDestroy()或更早版本中进行恢复。