简体   繁体   English

无法调用Android Studio中的TextToSpeech /无法正常工作

[英]TextToSpeech in Android Studio not being called / not working

The below simple code which makes an intent and calls another view group, is all from the basic first app tutorial. 下面的简单代码旨在实现意图并调用另一个视图组,全部来自基本的第一个应用程序教程。

https://developer.android.com/training/basics/firstapp/starting-activity.html https://developer.android.com/training/basics/firstapp/starting-activity.html

I used this question as a guide to getting TTS, and the app runs the toast and the view but skips the text to speech: Text to speech(TTS)-Android 我用这个问题作为获取TTS的指南,该应用程序运行了Toast和View,但跳过了文本到语音的操作: Text to speech(TTS)-Android

I then tried to add a TTS call/ class. 然后,我尝试添加一个TTS呼叫/类。 I am quite new to Android, so was wondering if someone could explain to me, or point me in the right direction about how to use the classes and build around them to help my general understanding, but overall a solution to this problem would be great, many thanks! 我对Android还是很陌生,所以想知道是否有人可以向我解释,或者为我指出如何使用这些类并围绕它们进行构建以帮助我全面理解的正确方向,但是总的来说,解决这个问题的方法是不错的, 非常感谢!

package com.example.ollie.myapplication3; 包com.example.ollie.myapplication3;

    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Build;
    import android.speech.tts.TextToSpeech;
    import android.support.annotation.RequiresApi;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.google.android.gms.appindexing.Action;
    import com.google.android.gms.appindexing.AppIndex;
    import com.google.android.gms.appindexing.Thing;
    import com.google.android.gms.common.api.GoogleApiClient;

    import java.util.Locale;

    public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

        public static final String EXTRA_MESSAGE = "com.example.myapplication.MESSAGE";
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        private GoogleApiClient client;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }

        /**
         * Called when user clicks send button
         */
        public void sendMessage(View view) {

            TextToSpeech tts = new TextToSpeech(this, this);
            tts.setLanguage(Locale.US);
            tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null, null);

            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_LONG;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            Intent intent = new Intent(this, DisplayMessageActivity.class);
            EditText editText = (EditText) findViewById(R.id.edit_message);
            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }

        @Override
        public void onInit(int status) {
            TextToSpeech tts = new TextToSpeech(this, this);
            tts.setLanguage(Locale.US);
            tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null, null);
        }
    }

Try to make your 'tts' variable a local one (in the activity) so you make sure you are using the same instance you have instantiated instead of creating a new one everytime you press the button. 尝试将“ tts”变量设为本地变量(在活动中),以确保您使用的是实例化的实例,而不是每次按下按钮时都创建一个新实例。

Like this: 像这样:

public class MainActivity extends AppCompatActivity {

private TextToSpeech tts;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                tts.setLanguage(Locale.US);
            }
        }
    });

}


public void sendMessage(View view) {
    tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null, null);

    ...
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM