禁用识别监听器的"就绪"声音

ian*_*mas 8 android

我在选项卡式片段活动中实现了RecognitionListener.什么情况是,当用户使用滚动,实现了听众的片段创建/销毁和我的Nexus 7和Galaxy Nexus的你听到相关的RegonitionListener声音,或者也许它的SpeechRecognizer,准备好使用.无论哪个类是声音的原因,我想允许用户禁用它.我意识到目前我的问题是我正在倾听而没有我正在解决的用户关注点; 但是,我仍然不希望默认听到声音,而是让用户选择加入通知声音.

所以我的问题是,是否可以禁用与监听器关联的通知声音?我一直无法在文档中找到这个.

Bil*_*ani 15

正如zapl所说,没有直接的方法可以做到这一点,但你可以尝试静音系统音量,正如你所说,腾讯说你避免任何形式的黑客攻击.然后我必须说没有方法可以做到这一点,但我仍然在为那些遭受同样问题的人分享这个技巧解决方案,并且可能不介意"黑客"技巧.只是节省他们的挫折时间.

将系统音量静音该特定时间,然后取消静音.这该怎么做:

在您的班级中,创建以下对象

private AudioManager mAudioManager;
private int mStreamVolume = 0; 
Run Code Online (Sandbox Code Playgroud)

在您的onCreate方法中执行以下操作.

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Run Code Online (Sandbox Code Playgroud)

当您开始收听语音识别时,请执行以下操作

speechRecognizer.startListening(intent); // this method make that annoying sound
mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); // getting system volume into var for later un-muting 
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // setting system volume to zero, muting
Run Code Online (Sandbox Code Playgroud)

在您的Speech RecognitionListener接口中,应该有一个onReadyForSpeech方法.

class MyRecognitionListener implements RecognitionListener          
{
  public void onReadyForSpeech(Bundle params)
  {
    // this methods called when Speech Recognition is ready
    // also this is the right time to un-mute system volume because the annoying sound played already
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mStreamVolume, 0); // again setting the system volume back to the original, un-mutting
  }
}
Run Code Online (Sandbox Code Playgroud)

这就是你如何用这种恼人的声音发挥技巧.显然任何其他声音也会静音一秒钟.

  • 我在函数“public void onResults(Bundle results)”中重置了音量。因为语音识别之后还有一个声音。 (2认同)