TTS: 语音合成失败

huangapple go评论108阅读模式
英文:

TTS: Speak fails

问题

我正在使用一个简单的代码来使用文本到语音

package ch.yourclick.kitt.fragments;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.Locale;
import ch.yourclick.kitt.R;

public class GeneralFragment extends Fragment {
    private TextToSpeech tts;

    public GeneralFragment() {
        // 必需的空公共构造函数
    }

    public static GeneralFragment newInstance() {
        GeneralFragment fragment = new GeneralFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_general, container, false);
        Button hello = view.findViewById(R.id.hello);

        tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(Locale.getDefault());
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "不支持的语言");
                    }
                }
                else {
                    Log.e("TTS", "" + status); // 返回 -1
                    Log.e("TTS", "" + TextToSpeech.SUCCESS); // 返回 0
                }
            }
        });

        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();
            }
        });
        return view;
    }

    private void speak() {
        String text = "你好";
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    @Override
    public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}

我收到以下消息:

W/TextToSpeech: speak failed: not bound to TTS engine

我的问题是文本到语音无法初始化:

status 返回 -1TextToSpeech.SUCCESS 返回 0

我正在使用 Android Studio,我的虚拟设备是 Pixel 2 API 30。所以似乎在其上安装了 Google 文本转语音引擎

TTS: 语音合成失败

设置 -> 辅助功能 -> 文本转语音输出

如果我点击 播放,我可以听到声音,所以我知道这不应该是设备问题。但为什么在我的应用上不起作用呢?我没有在上面看到任何错误。

我不知道我做错了什么。如果您知道答案或对可能的解决方法有任何建议,请告诉我!

英文:

I am using a simple code to use Text-To-Speech:

package ch.yourclick.kitt.fragments;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.Locale;
import ch.yourclick.kitt.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GeneralFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GeneralFragment extends Fragment {
    private TextToSpeech tts;

    public GeneralFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment General.
     */
    // TODO: Rename and change types and number of parameters
    public static GeneralFragment newInstance() {
        GeneralFragment fragment = new GeneralFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_general, container, false);
        Button hello = view.findViewById(R.id.hello);

        // Text to speech
        tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) { // <-- I never get into that if statement
                    int result = tts.setLanguage(Locale.getDefault());
                    // Language is not supported
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    }
                }
                else {
                    Log.e("TTS", "" + status); // Returns -1
                    Log.e("TTS", "" + TextToSpeech.SUCCESS); // Returns 0
                }
            }
        });

        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();
            }
        });
        return view;
    }

    /**
     * Speak
     */
    private void speak() {
        String text = "Hello";
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    /**
     * Turn off
     */
    @Override
    public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}

I get the message:

> W/TextToSpeech: speak failed: not bound to TTS engine

My problem is that Text To Speech cannot be initialized:

status returns -1 and TextToSpeech.SUCCESS returns 0.

I am using Android Studio and my virtual device is Pixel 2 API 30. So Google Text-to-speech Engine seems to be installed on it:

TTS: 语音合成失败

Settings -> Accessibility -> Text-to-speech output

If I click on Play, I hear a voice and so I know that it should not be a device issue. But why is it not working on my application? I do not get any errors on it.

I have no idea what I am doing wrong. If you know the answer or have any advice on what it could be, please let me know!

答案1

得分: 4

Google称:“针对Android 11的应用程序,使用文本到语音的应用应在其清单的查询元素中声明TextToSpeech.Engine#INTENT_ACTION_TTS_SERVICE。”

因此,请将以下内容添加到您的AndroidManifest.xml文件中:

<queries>
    <intent>
        <action android:name="android.intent.action.TTS_SERVICE" />
    </intent>
</queries>

<application>标签之前添加此内容对我有效。(尽管Android Studio显示“不允许在此处使用元素queries”)。

英文:

Google says "Apps targeting Android 11 that use text-to-speech should declare TextToSpeech.Engine#INTENT_ACTION_TTS_SERVICE in the queries elements of their manifest:"

So add this to your AndroidManifest.xml:

&lt;queries&gt;
&lt;intent&gt;
&lt;action android:name=&quot;android.intent.action.TTS_SERVICE&quot; /&gt;
&lt;/intent&gt;
&lt;/queries&gt;

Adding this before &lt;application tag worked for me. (Android Studio says Element queries is not allowed here though).

答案2

得分: 0

如日志中所述,“未绑定到TTS引擎”。在我的一侧,它只是被停用了,这就是它不能绑定到TTS引擎的原因。请在另一个帖子中查看我的回答,在此

英文:

As said by the log "not bound to TTS engine". On my side it was just deactivate and that was the reason that it couldn't bound to TTS engine.
See my answer in another thread, there

答案3

得分: 0

>speak failed: not bound to TTS engine<br>
>这可能不是一个 错误,只是打印一个普通的 日志

但是如果出现了这个日志,语音播报可能无法执行。

在查找了信息之后,我发现解决这个问题的方法是:
在实现了 OnInitListener 接口的 onInit 方法中重写语音播放功能:

@Override
public void onInit(int status) {
    textToSpeech.setSpeechRate(speechRate);
    textToSpeech.setPitch(pitch);
    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

通过这种方式,您可以避免出现 speak failed: not bound to TTS engine 错误。

英文:

>speak failed: not bound to TTS engine<br>
>This maybe not an error, just print an ordinary log.

But if this log appears, the voice announcement maybe cannot be performed

After searching for information, I found that the way to solve this reason is:
Over-write voice play function in the onInit method that implements the OnInitListener interface:

@Override
public void onInit(int status) {
			textToSpeech.setSpeechRate(speechRate);
            textToSpeech.setPitch(pitch);
			textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

By this way you can avoid the error of speak failed: not bound to TTS engine

huangapple
  • 本文由 发表于 2020年7月26日 03:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63092645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定