2014-10-10 136 views
1

我試圖通過文本語音轉換系統說出短語時使音頻播放器無聲。正如預期的那樣,音頻管理器會在請求音頻焦點時減小音量,但在放棄AudioFocus時,音量不會恢復,並且不會調用abandonAudioFocus的回調。 這是我的代碼:Audiomanager.abandonAudioFocus不會繼續播放

private class SpeakRunnable implements Runnable { 
    String mText; 

    public SpeakRunnable(String text) { 
     mText = text; 
    } 

    @Override 
    public void run() { 
     stream_volume = am.getStreamVolume(AudioManager.STREAM_MUSIC); 
     am.requestAudioFocus(new OnAudioFocusChangeListener() { 
      @Override 
      public void onAudioFocusChange(int focusChange) { 
        //Check if focus granted 
        ..... 
        HashMap<String, String> params = new HashMap<String, String>(); 
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "none"); 
        talker.speak(mText, TextToSpeech.QUEUE_ADD, params); 
      } 
     }, 
     AudioManager.STREAM_MUSIC, 
     AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); 

    } 
} 

和:

talker.setOnUtteranceProgressListener(new UtteranceProgressListener() { 

      @Override 
      public void onStart(String utteranceId) { 

      } 

      @Override 
      public void onError(String utteranceId) { 

      } 

      @Override 
      public void onDone(String utteranceId) { 
       am.abandonAudioFocus(new OnAudioFocusChangeListener() {      
        @Override 
        public void onAudioFocusChange(int focusChange) { 
         Log.v(app_id, "CALLBACK"); 
        } 
       }); 
      } 
     }); 

任何想法?提前致謝。

回答

2

您需要將您傳遞給requestAudioFocus的相同AudioManager.OnAudioFocusChangeListener傳遞給abandonAudioFocus。這在the documentation for abandonAudioFocus描述:

public int abandonAudioFocus (AudioManager.OnAudioFocusChangeListener l) 

Parameters 
l the listener with which focus was requested. 

另外,我不認爲你應該期望在監聽器onAudioFocusChange方法被調用爲你放棄音頻焦點的結果。當您放棄音頻焦點時,其他一些已請求音頻焦點的音頻焦點客戶端將收到已獲得焦點的通知。 如果你想了解這是如何在Android內部實現的,你可以看看MediaFocusControl source code

+0

你說得對。那是我的錯誤。 謝謝! – dantard 2014-10-28 14:11:44