服务中的文本转语音

发布于 2024-12-17 19:44:11 字数 2737 浏览 1 评论 0原文

我在 Service 中遇到 TTS 问题。它表现得好像想要说话,但它从来没有说话。观察 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();
        }

    }

}

谢谢

I've got a problem with TTS in a Service. It acts like it wants to talk but it never does. Watching the LogCat it prints "TTS received: the text it should speak" and I Log when it init's and that's showing success. I've tried creating a thread for it, that didnt help.
onUtteranceComplete never triggers either. I've even done a while loop like this (just for testing):

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

...and it's never speaking

I know TTS is setup correctly because it works in a regular Activity

Here's my code.

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

    }

}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

廻憶裏菂餘溫 2024-12-24 19:44:11

好的,我现在已经弄清楚了!发生的情况是它在初始化 TTS 之前尝试说话。因此,在一个线程中,我等待准备好不 == 999。一旦它为 1 或其他任何值,我们将负责说话。将其放入 while 循环中可能不安全,但是......它仍然有效。

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

    }


}

Ok, I've got it figured out now! What was happening is it was trying to speak before TTS was initialized. So in a thread I wait for ready to not == 999. Once its either 1 or anything else we'll then take care of speaking. This might not be safe putting it in a while loop but... It's working nonetheless.

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

    }


}
最近可好 2024-12-24 19:44:11

我刚刚遇到了类似的问题 - 这是由于在 onInit() 启动 TTS 之前调用了 mTTS.speak。我的处理方式略有不同,将 mTTS.speak() 放在一个单独的函数中,然后从 onInit if (TextToSpeech.Successful) 中调用该函数,因此播放语音 <启动之后并且启动之前(来自onStart()等)。

I've just had a similar issue - It was due to mTTS.speak being called before TTS was initiated by onInit(). I tackled it slightly differently by placing mTTS.speak() in a separate function and then calling that function from within onInit if (TextToSpeech.Successful), therefore playing the speech after it is initiated and not before (from onStart() ect).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文