Android實現自動朗讀功能(TTS)


前言: Android提供了自動朗讀支持。可以對指定文本內容進行朗讀,從而發生聲音;還允許把文本對應的音頻錄制成音頻文件,方便以后播放。Android的自動朗讀主要通過TextToSpeech來完成,構造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);當創建TextToSpeech對象時,必須先提供一個OnInitListener監聽器——負責監聽TextToSpeech的初始化結果。

效果圖如下:

使用TextToSpeech的步驟如下:

1、創建TextToSpeech對象,創建時傳入OnInitListener監聽器監聽示范創建成功。
2、設置TextToSpeech所使用語言國家選項,通過返回值判斷TTS是否支持該語言、國家選項。
3、調用speak()或synthesizeToFile方法。
4、關閉TTS,回收資源。

布局文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
     xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:orientation = "vertical" >
 
     < EditText
         android:id = "@+id/input_text"
         android:layout_marginTop = "20dp"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content" />
 
     < LinearLayout
         android:layout_marginTop = "10dp"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content" >
 
         < Button
             android:id = "@+id/speech"
             android:text = "Speech"
             android:layout_width = "wrap_content"
             android:layout_weight = "1"
             android:layout_height = "wrap_content" />
 
         < Button
             android:id = "@+id/record"
             android:text = "Record"
             android:layout_weight = "1"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content" />
 
     </ LinearLayout >
 
</ LinearLayout >

Activity文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class SpeechActivity extends AppCompatActivity {
 
     private EditText input;
     private Button speech,record;
 
     private TextToSpeech textToSpeech;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_speech);
 
         textToSpeech = new TextToSpeech( this , new TextToSpeech.OnInitListener() {
             @Override
             public void onInit( int status) {
                 if (status == textToSpeech.SUCCESS) {
                     int result = textToSpeech.setLanguage(Locale.CHINA);
                     if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                             && result != TextToSpeech.LANG_AVAILABLE){
                         Toast.makeText(SpeechActivity. this , "TTS暫時不支持這種語音的朗讀!" ,
                                 Toast.LENGTH_SHORT).show();
                     }
                 }
             }
         });
 
         input = (EditText) findViewById(R.id.input_text);
         speech = (Button) findViewById(R.id.speech);
         record = (Button) findViewById(R.id.record);
 
         speech.setOnClickListener( new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 textToSpeech.speak(input.getText().toString(),
                         TextToSpeech.QUEUE_ADD, null );
             }
         });
 
         record.setOnClickListener( new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 String inputText = input.getText().toString();
                 HashMap<String, String> myHashRender = new HashMap<>();
                 myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);
                 textToSpeech.synthesizeToFile(inputText, myHashRender,
                         "/mnt/sdcard/my_recorder_audios/sound.wav" );
                 Toast.makeText(SpeechActivity. this , "聲音記錄成功。" , Toast.LENGTH_SHORT).show();
             }
         });
     }
 
     @Override
     protected void onDestroy() {
         if (textToSpeech != null )
             textToSpeech.shutdown();
         super .onDestroy();
     }
}

這里我們使用的是中文,int result = textToSpeech.setLanguage(Locale.CHINA);你也可以根據自己的需求更改為其他支持的語言。

最后在AndroidManifest.xml中加入權限:

1
2
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
     < uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />

總結:通過使用Android提供的TTS,我們可以對指定文本內容進行朗讀,從而發生聲音;還允許把文本對應的音頻錄制成音頻文件,保存到本地短鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM