服务中的TTS

时间:2015-06-29 23:58:50

标签: android service broadcastreceiver text-to-speech

我正在播放播音员项目。我的TTS服务不会说内部服务。我的logcat说它运行它但不能说出来。这是我的代码。请告诉我出了什么问题? 我是从broadcastreciever调用它。

public class Speaker extends Service implements TextToSpeech.OnInitListener {
    public static TextToSpeech mtts;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d("SpeakerService", "Service created successfully!");
        mtts = new TextToSpeech(getApplicationContext(), this);
        mtts.setLanguage(Locale.ENGLISH);

    }

    @Override
    public void onStart(Intent intent, int startid) {
        Log.d("SpeakerService", "Service started successfully!");
        Log.d("SpeakerService", "Service started successfully!");
        // Log.d("SpeakerService", "tspker.mtts = " +
        // TextSpeaker.mtts.toString());
        mtts = new TextToSpeech(getApplicationContext(), this);
        mtts.setLanguage(Locale.ENGLISH);
        mtts.speak("The service has been done The service has been done",
                TextToSpeech.QUEUE_FLUSH, null);
        Toast.makeText(getApplicationContext(),
                "The service has been done!", Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onDestroy() {
        if (mtts != null) {
            mtts.stop();
            Toast.makeText(getApplicationContext(),
                    "The service has been destroyed!", Toast.LENGTH_SHORT)
                    .show();
        }

    }

    @Override
    public void onInit(int arg0) {

    }

}

1 个答案:

答案 0 :(得分:0)

您必须在speakonInit之后致电onInit