Search code examples
androidmultithreadingtext-to-speech

Android: Coordinate SounPool and TTS


I define this simple method in my 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();
        }
    });

Where speakOut() is this:

private void speakOut() {

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

But this reproduce my mp3 file and my tts speak at same time.

So the question is:

How can I reproduce tts after my mp3?


Solution

  • I solve the problem in this way:

    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);
    
    }
    

    Even if this require the knowledge of duration of mp3 file.