Android:协调SounPool和TTS
我在活动中定义了这个简单的方法:Android:协调SounPool和TTS,android,multithreading,text-to-speech,Android,Multithreading,Text To Speech,我在活动中定义了这个简单的方法: 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,
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?您应该知道声音文件的持续时间
R.raw.water\u bounding
,设置此持续时间的倒计时,并在onFinish()中调用speakOut()
我用以下方法解决问题:
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文件的持续时间。I'v从未使用过这个,但可能就是那个。您是否可以添加if条件status==0?您可能还想看看这一点