服务中的TextToSpeech

时间:2011-11-25 20:39:05

标签: android service text-to-speech

我在TTS中遇到了Service的问题。它的行为就像它想说话,但它永远不会。看着LogCat它会打印出“收到的TTS:它应该说的文字”,并且当它初始化时我记录并显示成功。我已经尝试为它创建一个线程,但没有帮助。 onUtteranceComplete永远不会触发。我甚至做过这样的while循环(仅用于测试):

while(mTTS.isSpeaking()) {
      Log.d("", "speaking");
}

......它永远不会说话

我知道TTS设置正确,因为它适用于常规Activity

这是我的代码。

import java.util.HashMap;
import java.util.Locale;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;

public class TTSService extends Service implements OnInitListener, OnUtteranceCompletedListener {
TextToSpeech mTTS;

    @Override
    public void onCreate() {
        Log.d("", "TTSService Created!");
        mTTS = new TextToSpeech(getApplicationContext(), this);


        //I've tried it in a thread....
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> myHashStream = new HashMap<String, String>();
                myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
                myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1");

                mTTS.setLanguage(Locale.US);
                //mTTS.setOnUtteranceCompletedListener(this);
                mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);        
            }

        }).start();*/

        //I've tried it not in a thread...
        HashMap<String, String> myHashStream = new HashMap<String, String>();
        myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
        myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1");

        mTTS.setLanguage(Locale.US);
        mTTS.setOnUtteranceCompletedListener(this);
        mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);    

    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onInit(int status) {
        Log.d("", "TTSService onInit: " + String.valueOf(status));
        if(status == TextToSpeech.SUCCESS){
            Log.d("", "TTS Success");
        }
    }

    public void onUtteranceCompleted(String uttId) {
        Log.d("", "done uttering");
        if(uttId == "1") {
            mTTS.shutdown();
        }

    }

}

由于

2 个答案:

答案 0 :(得分:3)

好的,我现在已经弄明白了!发生的事情是它在TTS初始化之前试图发言。所以在一个线程中我等待准备好不要== 999.一旦它的1或其他任何东西我们将负责说话。这可能不安全,但它仍然存在但是......它仍然有效。

import java.util.HashMap;
import java.util.Locale;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;

public class TTSService extends Service implements OnInitListener, OnUtteranceCompletedListener {
TextToSpeech mTTS;
int ready = 999;
    @Override
    public void onCreate() {

        Log.d("", "TTSService Created!");
        mTTS = new TextToSpeech(getApplicationContext(), this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(ready == 999) {
                    //wait
                }
                if(ready==1){
                HashMap<String, String> myHashStream = new HashMap<String, String>();
                myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
                myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1");

                mTTS.setLanguage(Locale.US);
                //mTTS.setOnUtteranceCompletedListener(this);
                mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);

                } else { 
                    Log.d("", "not ready"); 
                    }
            }

        }).start();


        stopSelf();

    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onDestroy() {
        mTTS.shutdown();
        super.onDestroy();
    }
    @Override
    public void onInit(int status) {
        Log.d("", "TTSService onInit: " + String.valueOf(status));
        if (status == TextToSpeech.SUCCESS)
        {
            ready = 1;

        } else {
            ready = 0;
            Log.d("", "failed to initialize");
        }

    }

    public void onUtteranceCompleted(String uttId) {
        Log.d("", "done uttering");
        if(uttId == "1") {
            mTTS.shutdown();
        }

    }


}

答案 1 :(得分:2)

我刚刚遇到了类似的问题 - 这是由于在mTTS.speak启动TTS之前调用了onInit()。我将mTTS.speak()放在一个单独的函数中,然后在onInit if (TextToSpeech.Successful)内调用该函数,从而稍微改变了它,因此在之后播放语音并且不是< / strong>之前(来自onStart()等)。