Android:协调SounPool和TTS

时间:2013-04-23 07:11:33

标签: android multithreading text-to-speech

我在Activity中定义了这个简单的方法:

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?

2 个答案:

答案 0 :(得分:1)

您应该知道声音文件R.raw.water_boiling的持续时间,设置此持续时间的倒数计时器,并在speakOut()中拨打onFinish()

答案 1 :(得分: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文件的持续时间。