gpt4 book ai didi

java - Android Studio 中的 TextToSpeech 未被调用/无法工作

转载 作者:行者123 更新时间:2023-12-02 03:00:53 24 4
gpt4 key购买 nike

下面的简单代码生成 Intent 并调用另一个 View 组,全部来自基本的第一个应用程序教程。

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

我使用这个问题作为获取 TTS 的指南,应用程序运行 toast 和 View ,但跳过文本到语音: Text to speech(TTS)-Android

然后我尝试添加 TTS 通话/类(class)。我对 Android 很陌生,所以想知道是否有人可以向我解释,或者为我指明如何使用这些类并围绕它们进行构建的正确方向,以帮助我进行一般理解,但总的来说,解决这个问题会很棒,非常感谢!

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

最佳答案

尝试将“tts”变量设置为本地变量(在 Activity 中),以便确保使用已实例化的同一实例,而不是每次按下按钮时都创建一个新实例。

像这样:

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

...
}

}

关于java - Android Studio 中的 TextToSpeech 未被调用/无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42377116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com