2012-08-14 60 views
7

我在選項卡式片段活動中實施RecognitionListener。會發生什麼是當用戶滾動時,實現監聽器的片段被創建/銷燬,並且在我的Nexus 7和Galaxy Nexus上,您會聽到與RegonitionListener關聯的聲音,或者可能已經準備好使用它的SpeechRecognizer。無論類別是聲音的原因,我希望允許用戶禁用它。我意識到目前我的問題是,我正在傾聽,而沒有我正在解決的用戶焦點;但是,我仍然不希望聽到默認聲音,並希望讓用戶選擇通知聲音。禁用識別監聽器的「就緒」聲音

所以我的問題是,是否有可能禁用與監聽器相關的通知聲音?我一直無法在文檔中找到它。

+2

我想聲音是由語音識別服務產生的,這意味着你的應用程序不太可能改變這種情況,特別是因爲我沒有在系統設置或官方語音搜索彈出窗口中看到有關該聲音的任何選項。您可以在語音識別過程中嘗試將音量靜音,但也可以將可能正在進行的背景音樂靜音。 – zapl 2012-08-14 12:12:54

+0

當識別監聽器啓動時,背景音樂會被切斷。如果可能的話,我寧願避免使用黑客。道具的想法! – 2012-08-14 12:18:06

回答

14

由於zapl表示沒有直接的方法可以做到這一點,但您可以嘗試將系統音量設置爲靜音,並且與您一樣,騰訊表示您正在避免任何形式的黑客攻擊。那麼我必須說,沒有辦法可以做到這一點,但我仍然在分享這個訣竅解決方案,以幫助那些遭遇同樣問題並且不會介意「黑客」技巧的人。只是節省他們的小時數的挫折。

將該特定時間的系統音量靜音,然後取消靜音。如何做到這一點:

在你的類,創建下列對象

private AudioManager mAudioManager; 
private int mStreamVolume = 0; 

在你的onCreate方法做以下。

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

當你開始監聽語音識別,就以下

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 

在你的演講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 
    } 
} 

這就是你如何玩這個惱人的聲音的伎倆。顯然任何其他聲音也會靜音一秒鐘。

+1

我重置了函數'public void onResults(Bundle results)'中的體積。因爲語音識別後還有另一種聲音。 – William 2015-08-25 16:00:43

3

以前的答案是可以的,但它會因爲NullPointer異常(在空對象上調用getStreamVolume)而崩潰。爲了解決這個問題,該getStreamVolume行添加前:

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

0

如果你想stop the end sound爲好,你可以取消靜音在onResultsonError代替onReadyForSpeech

創建一個處理程序作爲一個成員變量:

private Handler mHandler = new Handler(); 

RecognitionListener方法:

@Override 
public void onResults(Bundle results) { 
    // Your logic here 
    startAudioSound(); 
} 

@Override 
public void onError(int errorCode) { 
    // Your logic here 
    startAudioSound(); 
} 

private void startAudioSound(long delay) { 
    mHandler.postDelayed(() -> { 
     mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mStreamVolume, 0); // again setting the system volume back to the original, un-mutting 
    }, 300); 
} 

我已經有300毫秒的延遲測試,它的工作原理。較短的延遲也可能起作用。