TTS 内部服务

TTS inside Service

我正在做播音员项目。我的 TTS 服务不会在服务内部说话。我的 logcat 说它可以运行但不能说话。这是我的代码。请告诉我出了什么问题? 我从广播接收器调用它。

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) {

    }

}

您必须在 onInit 中或在调用 onInit 之后调用 speak