Android TextToSpeech教程
在Android中,你可以通過 TextToSpeech 類將文本轉換為語音。愛掏網 - it200.com完成轉換后,你可以播放或創建聲音文件。愛掏網 - it200.com
TextToSpeech類的構造方法
- TextToSpeech(Context context, TextToSpeech.OnInitListener)
TextToSpeech類的方法
TextToSpeech類常用的方法如下:
方法 | 描述 |
---|---|
**int speak (String text, int queueMode, HashMap params) ** | 將文本轉換為語音。愛掏網 - it200.com隊列模式可以是QUEUE_ADD或QUEUE_FLUSH。愛掏網 - it200.com請求參數可以為空,也可以是KEY_PARAM_STREAM,KEY_PARAM_VALUME等。愛掏網 - it200.com |
int setSpeechRate(float speed) | 設置語音的速度。愛掏網 - it200.com |
int setPitch(float speed) | 設置語音的音調。愛掏網 - it200.com |
int setLanguage (Locale loc) | 設置語音的具體區域特定語言。愛掏網 - it200.com |
void shutdown() | 釋放由TextToSpeech Engine設置的資源。愛掏網 - it200.com |
int stop() | 中斷當前的語音輸出(無論是播放還是保存到文件),并丟棄隊列中的其他語音輸出。愛掏網 - it200.com |
HTML格式的英文文本翻譯成中文保留HTML格式不變輸出
您需要實現TextToSpeech.OnInitListener接口,以執行TextToSpeech引擎上的事件處理。愛掏網 - it200.com
TextToSpeech.OnInitListener接口的方法
此接口中只有一個方法。愛掏網 - it200.com
方法 | 描述 |
---|---|
void onInit (int status) | 在TextToSpeech引擎初始化完成后調用。愛掏網 - it200.com狀態可以是SUCCESS或ERROR。愛掏網 - it200.com |
Android TextToSpeech 示例
讓我們編寫代碼將文本轉換為語音。愛掏網 - it200.com
activity_main.xml
在布局中拖動一個文本視圖,一個編輯框和一個按鈕。愛掏網 - it200.com現在activity_main.xml文件將會是這樣的:
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="77dp"
android:layout_marginTop="42dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="59dp"
android:layout_marginTop="39dp"
android:text="Speak" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="Enter Text:" />
</RelativeLayout>
Activity類
讓我們來看看如何編寫代碼來朗讀給定的文本。愛掏網 - it200.com
package com.example.texttospeech;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button buttonSpeak;
private EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
buttonSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
buttonSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。