Android API开发之TTS开发之Android TTS简单使用
? ?Android提供了自動朗讀支持。可以對指定文本內容進行朗讀,從而發生聲音;還允許把文本對應的音頻錄制成音頻文件,保存到本地,方便以后播放。Android的自動朗讀主要通過TextToSpeech來完成。
? ?構造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);當創建TextToSpeech對象時,必須先提供一個OnInitListener監聽器——負責監聽TextToSpeech的初始化結果。
?
?
1.代碼
1.1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:background="#FFFFFF"
? ? android:orientation="vertical">
? ? <TextView
? ? ? ? android:id="@+id/txt_content"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="90dp"
? ? ? ? android:gravity="center"
? ? ? ? android:text="播放聲音"
? ? ? ? android:textColor="#339BFF"
? ? ? ? android:textSize="14sp" />
? ? <TextView
? ? ? ? android:id="@+id/txt_contents"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="90dp"
? ? ? ? android:gravity="center"
? ? ? ? android:text="保存文本"
? ? ? ? android:textColor="#339BFF"
? ? ? ? android:textSize="14sp" />
?
</LinearLayout>
?
1.2.java代碼
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
? ? ? ? TextView txt_contents = (TextView) view.findViewById(R.id.txt_contents);
? ? ? ? textToSpeech = new TextToSpeech(MyFragment1.this.getContext(), new TextToSpeech.OnInitListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onInit(int status) {
? ? ? ? ? ? ? ? if (status == textToSpeech.SUCCESS) {
? ? ? ? ? ? ? ? ? ? int result = textToSpeech.setLanguage(Locale.ENGLISH);
? ? ? ? ? ? ? ? ? ? if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
? ? ? ? ? ? ? ? ? ? ? ? ? ? && result != TextToSpeech.LANG_AVAILABLE){
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MyFragment1.this.getContext(), "TTS暫時不支持這種語音的朗讀!",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //播放語音
? ? ? ? txt_content.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? textToSpeech.speak(content, TextToSpeech.QUEUE_ADD, null);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //保存語音
? ? ? ? txt_contents.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? HashMap<String, String> myHashRender = new HashMap<>();
? ? ? ? ? ? ? ? myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, content);
? ? ? ? ? ? ? ? textToSpeech.synthesizeToFile(content, myHashRender,
? ? ? ? ? ? ? ? ? ? ? ? "/CDSP/pictures/sound.wav");
? ? ? ? ? ? ? ? Toast.makeText(MyFragment1.this.getContext(), "聲音記錄成功。", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? }
? ? ? ? });
?
?
2.效果圖
?
3.核心代碼講解
?
使用TextToSpeech的步驟如下:
創建TextToSpeech對象,創建時傳入OnInitListener監聽器監聽示范創建成功。
設置TextToSpeech所使用語言國家選項,通過返回值判斷TTS是否支持該語言、國家選項。
調用speak()或synthesizeToFile方法。
關閉TTS,回收資源。
?
總結
以上是生活随笔為你收集整理的Android API开发之TTS开发之Android TTS简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年R1快开门式压力容器操作最新解
- 下一篇: android 六棱形分析图,Andro