Android - Android服务中的文本到语音不起作用

时间:2015-07-17 17:27:11

标签: android service text-to-speech

我正在开发一个在服务器中使用文本到语音的应用程序,显然它不起作用。我已经检查了谷歌和其他答案,但我仍然不完全理解我遇到的问题。我已经完成了标准程序,但我还是比较新的android,所以可能会有一些我错过的东西。

以下是服务本身的代码:

package com.wordpress.chsiction.timesound;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

import java.util.Locale;


public class TimeSoundService extends Service implements TextToSpeech.OnInitListener {

private TextToSpeech tts;
private boolean isLoaded;

@Override
public void onCreate() {
    super.onCreate();
    tts = new TextToSpeech(getApplicationContext(), this);
    Log.d("TimeSoundService", "onCreate() ended");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, R.string.toast_startService, Toast.LENGTH_SHORT).show();

    Log.d("TimeSoundService", "Service started");

        String s = "String tts";

    Log.d("TimeSoundService", "String s = " + s);

    int x = 5;

    while (isLoaded && x > 0) {
        speak("test");
        Log.d("TimeSoundService", "Queue added");
        x--;
    }

    return Service.START_NOT_STICKY;
}

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

@Override
public void onDestroy () {
    super.onDestroy();
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    Log.d("TimeSoundService", "Service stopped");
    Toast.makeText(this, R.string.toast_stopService, Toast.LENGTH_SHORT).show();
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        int result = tts.setLanguage(Locale.US);
        if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
            isLoaded = true;
        } else {
            Log.d("TimeSoundService", "Language has missing data or is not supported");
            Toast.makeText(this, "Missing data", Toast.LENGTH_SHORT).show();
        }
    } else {
        Log.d("TimeSoundService", "Status unsuccessful");
        Toast.makeText(this, "Status unsuccessful", Toast.LENGTH_SHORT).show();
    }
}

private void speak(String s) {
    if (tts != null) {
        tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
    }
}
}

问题是这个 - 我不认为tts已经启动了。 isLoaded在运行后仍然为false,并且OnInit没有错误日志。 OnCreate跑了,这意味着tt也应该启动,但没有发生任何事情。另外我注意到onStartCommand中的日志有时没有出现。

编辑:刚刚发现演讲很少启动,而且只是一瞬间......

以下是日志:

07-17 16:55:15.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 16:55:30.190    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
07-17 16:55:34.950    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 16:55:48.750    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
07-17 16:55:49.660    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 16:55:49.670    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
07-17 16:55:49.670    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@3ba062d9
07-17 16:55:49.670    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
07-17 17:16:57.090    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
07-17 17:16:57.090    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@3ba062d9
07-17 17:16:57.090    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
07-17 17:16:57.110    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Queue added
07-17 17:16:59.470    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
07-17 17:16:59.470    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@3ba062d9
07-17 17:16:59.470    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
07-17 17:16:59.490    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Queue added
07-17 17:17:01.530    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
07-17 17:17:02.930    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 17:17:16.860    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 17:17:23.030    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
07-17 17:17:28.220    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 17:17:34.120    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
07-17 17:17:37.420    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 17:17:37.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
07-17 17:17:37.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@39d9f6bd
07-17 17:17:37.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
07-17 17:17:42.180    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
07-17 17:17:57.110    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
07-17 17:18:04.080    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped

MainActivity具有启动和停止服务的按钮,就是这样。这个问题很烦人,我已经实现了服务和类似的东西,但也许tts服务立即关闭(?)。希望你好先生可以帮助我,提前谢谢。

更新:我弄乱了代码并意识到onInitonCreate()之后执行onStartCommand,这就是为什么演讲没有'执行。这是我的问题 - 如何让onInit先运行或在addQueue代码初始化后运行?

2 个答案:

答案 0 :(得分:0)

我在演讲前通过短暂的延迟解决了我自己的问题。这只是一种临时方法,但我的最终应用程序将有足够的时间让tts初始化,因为它不会立即开始讲话。

答案 1 :(得分:0)

你不能让OnInit先运行"重点是它是同步进程的回调而onCreate和onStartCommand是生命周期回调,所以没有人能够控制它们执行的时间或顺序。

你需要做些什么来使你的代码正确在onStartCommand中检查TTS是否已经正确完成初始化,如果还没有(很可能)那么排队你的speak()调用稍后运行 - 即。当onInit()实际执行时状态为SUCCESS。