以下简单的代码构成了一个intent并调用另一个视图组,这些代码都来自基本的第一个应用程序教程。
https://developer.android.com/training/basics/firstapp/starting-activity.html
我使用这个问题作为获取TTS的指南,应用程序运行toast和视图但跳过文本到语音: Text to speech(TTS)-Android
然后我尝试添加TTS调用/类。我对Android很新,所以想知道是否有人可以向我解释,或者指出我正确的方向如何使用这些类并围绕它们构建以帮助我的一般理解,但总体来说这个问题的解决方案会很好非常感谢!
package 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);
}
}
答案 0 :(得分:2)
尝试制作你的'变量本地变量(在活动中),因此您确保使用已实例化的同一实例,而不是每次按下按钮时都创建一个新实例。
像这样:
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);
...
}
}