【问题标题】:Need to shut off the sound MediaRecorder plays when the state changes状态改变时需要关闭MediaRecorder播放的声音
【发布时间】:2013-12-18 04:00:03
【问题描述】:

我尝试了以下链接中的更改,但无济于事。

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

还有其他建议吗?

【问题讨论】:

  • 那么你正在播放音乐,当你录制唱片时应该怎么停止呢?
  • 不,我正在按顺序录制一系列视频并将它们保存到一个文件中,以便我可以分段下载整个视频。 MediaRecorder 在每个视频的开头播放快门声。我正在尝试关闭它,这样它就不会在序列中一遍又一遍地响起。

标签: android cordova audio mute


【解决方案1】:

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() 或更早的时间中恢复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多