2013-01-16 104 views
2

我試過在下面的鏈接中找到的更改,但無濟於事。需要關閉MediaRecorder在狀態更改時播放的聲音

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

是否有任何其他建議?

+0

因此,您在播放音樂以及在做記錄時應該停止播放什麼? –

+0

不,我正在按順序錄制一系列視頻並將它們保存到文件中,以便我可以分段下載整個視頻。 MediaRecorder在每個視頻開始時播放快門聲音。我試圖關閉它,以免它在序列中一遍又一遍地響徹。 –

回答

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()或更早的版本中進行恢復。

+0

不適用於我 – suitianshi