[英]TextToSpeech in Android Studio not being called / not working
下面的簡單代碼旨在實現意圖並調用另一個視圖組,全部來自基本的第一個應用程序教程。
https://developer.android.com/training/basics/firstapp/starting-activity.html
我用這個問題作為獲取TTS的指南,該應用程序運行了Toast和View,但跳過了文本到語音的操作: Text to speech(TTS)-Android
然后,我嘗試添加一個TTS呼叫/類。 我對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”變量設為本地變量(在活動中),以確保您使用的是實例化的實例,而不是每次按下按鈕時都創建一個新實例。
像這樣:
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.