2013-04-23 77 views
0

我在我的活動定義這個簡單的方法:安卓:座標SounPool和TTS

private void playSound(final boolean ttsOn) { 
    // TODO Auto-generated method stub 
    int water = 0;  
    SoundPool pl; 
    pl = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); 

    water = pl.load(this, R.raw.water_boiling, 0); 

    pl.setOnLoadCompleteListener(new OnLoadCompleteListener() { 

     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
      // TODO Auto-generated method stub 
      soundPool.play(sampleId, 1f, 1f, 0, 0, 1); 
      if(ttsOn) 
       speakOut(); 
     } 
    }); 

哪裏speakOut()是這樣的:

private void speakOut() { 

    tts.setLanguage(Locale.ITALIAN); 
    tts.speak(n.Message, TextToSpeech.QUEUE_FLUSH, null); 
} 

但這重現我的MP3文件和我的TTS在同時講話。

所以問題是:

如何在我的mp3後重現tts?

+0

我從來沒有使用過,但也許就是這樣。你可以添加if條件狀態== 0 http://developer.android.com/reference/android/media/SoundPool.OnLoadCompleteListener.html – PedroAGSantos 2013-04-23 07:19:04

+0

也你可能想看看這個http://stackoverflow.com /問題/ 4436055 /打的Android的Soundpool,得到通知的,當-結束 – PedroAGSantos 2013-04-23 07:25:11

回答

0

我解決這樣的問題:

private void playSound(final boolean ttsOn) { 
    // TODO Auto-generated method stub 
    int water = 0;  
    SoundPool pl; 
    pl = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); 

    water = pl.load(this, R.raw.system_alert_003, 0); 



    pl.setOnLoadCompleteListener(new OnLoadCompleteListener() { 

     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
      // TODO Auto-generated method stub 
      soundPool.play(sampleId, 1f, 1f, 0, 0, 1); 
      if(ttsOn) 
      { 
       myMediaTimer = new Timer(); 
       myMediaTimer.schedule(new TimerTask() {   
        @Override 
        public void run() { 
         speakOut(); 
        } 

       }, DURATION_MEDIA_FILE); 

      } 
     } 
    }); 


    Log.i("onCrete","WATER FILE: "+water); 

} 

即使這需要MP3文件的持續時間的知識。

1

您應該知道聲音文件R.raw.water_boiling的持續時間,在此持續時間內設置倒數計時器,並撥打speakOut(),onFinish()