文本 To 音频
文本 ?To ?音頻
?
TextToSpeech介紹
TextToSpeech,簡稱 TTS,是Android 1.6版本中比較重要的新功能。將所指定的文本轉成不同語言音頻輸出。它可以方便的嵌入到游戲或者應用程序中,增強用戶體驗。
TTS Engine,依托于當前Android Platform所支持的幾種主要語言:English、French、German、Italian及Spanish五大語言(暫時沒有直接中文)。TTS可以將文本隨意的轉換成以上任意五種語言的語音輸出。
????????
布局設計
界面中組件的設計很簡單,分為四個部分:
1、內容編輯框,供用戶輸入需要轉換成語音的文本;
2、音頻播放按鈕,點擊后播放文本對應的音頻;
3、語調編輯框,供用戶調節朗讀的語調,支持浮點型數據,默認值為1.0;
4、語速編輯框,供用戶調節語速的語速,只能為整型數據,默認值為1;
代碼如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:gravity="center_vertical" 7 tools:context="com.texttospeechtest.MainActivity" > 8 9 <LinearLayout 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:orientation="horizontal" 13 android:layout_gravity="center_horizontal" 14 android:gravity="center_horizontal" > 15 16 <TextView 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="@string/speak_content" 20 android:textColor="#f00" /> 21 22 <EditText android:id="@+id/input_something" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:hint="@string/input_something" /> 26 27 </LinearLayout> 28 29 <Button android:id="@+id/to_speak" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_gravity="center_horizontal" 33 android:layout_marginTop="40dp" 34 android:text="@string/to_speak" /> 35 36 <Button android:id="@+id/save_wav" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_gravity="center_horizontal" 40 android:layout_marginTop="40dp" 41 android:text="@string/save_wav" /> 42 43 <LinearLayout 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:orientation="horizontal" 47 android:layout_gravity="center_horizontal" 48 android:gravity="center_horizontal" 49 android:layout_marginTop="40dp" > 50 51 <TextView 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 android:text="@string/pitch_level_name" 55 android:textColor="#f00" /> 56 57 <EditText android:id="@+id/pitch_level" 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:hint="@string/pitch_level" 61 android:text="@string/pitch_level" /> 62 63 </LinearLayout> 64 65 <LinearLayout 66 android:layout_width="wrap_content" 67 android:layout_height="wrap_content" 68 android:orientation="horizontal" 69 android:layout_gravity="center_horizontal" 70 android:gravity="center_horizontal" 71 android:layout_marginTop="40dp" > 72 73 <TextView 74 android:layout_width="wrap_content" 75 android:layout_height="wrap_content" 76 android:text="@string/speak_rate_name" 77 android:textColor="#f00" /> 78 79 <EditText android:id="@+id/speak_rate" 80 android:layout_width="wrap_content" 81 android:layout_height="wrap_content" 82 android:hint="@string/speak_rate" 83 android:text="@string/speak_rate" /> 84 85 </LinearLayout> 86 87 </LinearLayout>其中用到的字符串定義在values/strings.xml文件中:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">TextToSpeechTest</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 <string name="speak_content">Speak Content: </string> 8 <string name="input_something">Input Something</string> 9 <string name="to_speak">To Speak</string> 10 <string name="pitch_level_name">Pitch Level(float): </string> 11 <string name="pitch_level">1.0</string> 12 <string name="speak_rate_name">Speak Rate(int): </string> 13 <string name="speak_rate">1</string> 14 15 </resources>?
TTS應用
1、在應用程序中實現TTS的TextToSpeech.OnInitListener監聽器,測試案例針對onInit(int status)和onDestroy()兩個方法進行了重載。代碼如下:
1 @Override 2 public void onInit(int status) { 3 // TODO Auto-generated method stub 4 if (status == TextToSpeech.SUCCESS) { 5 int result = tts.setLanguage(Locale.US); 6 7 if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { 8 Log.e("TTS", "This Language is not supported"); 9 speak.setEnabled(false); 10 } else { 11 speak.setEnabled(true); 12 } 13 } else { 14 Log.e("TTS", "Initilization Failed!"); 15 } 16 } 1 @Override 2 public void onDestroy() { 3 // Don't forget to shutdown tts! 4 if (tts != null) { 5 tts.stop(); 6 tts.shutdown(); 7 } 8 super.onDestroy(); 9 }2、對TTS、Button及EditText組件的對象進行初始化,是在主類MainActivity的重載方法onCreate(Bundle savedInstanceState) 中實現,代碼如下:
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 6 tts = new TextToSpeech(this, this); 7 content = (EditText)findViewById(R.id.input_something); 8 speak = (Button)findViewById(R.id.to_speak); 9 10 pitch = (EditText)findViewById(R.id.pitch_level); 11 rate = (EditText)findViewById(R.id.speak_rate); 12 13 speak.setOnClickListener(new OnClickListener(){ 14 @Override 15 public void onClick(View v) { 16 // TODO Auto-generated method stub 17 ToSpeak(); 18 } 19 20 });21 }注意,在定義TTS對象tts時會對上面定義的onInit(int status)方法進行調用,這個過程非常關鍵。本案例中讓其完成的事情包括:
A、判斷初始化狀態;
B、若成功,設置文本轉換語言環境;
C、若語言環境存在并支持,則繼續.....
3、點擊音頻播放按鈕后,響應方法為ToSpeak(),其實現如下:
1 public void ToSpeak(){ 2 //to speak input content 3 String c = content.getText().toString(); 4 if(c.equals("")){ 5 Toast.makeText(this, "Please input something first.", Toast.LENGTH_SHORT).show(); 6 } 7 else{ 8 //set speak pitch 9 Float p = Float.valueOf(pitch.getText().toString()); 10 tts.setPitch(p); 11 12 //set speak rate 13 int r = Integer.valueOf(rate.getText().toString()); 14 tts.setSpeechRate(r); 15 16 tts.speak(c, TextToSpeech.QUEUE_FLUSH, null); 17 } 18 }這里的代碼就好理解了,主要是在獲取用戶輸入的文本、語調及語速之后,進行音頻的播放。注意:方法開頭對文本編輯框中的內容進行了空字串(“” )判斷,若為空則沒有播放的必要。
?
結果圖
雖然界面很簡陋,還是附上幾張圖片把,畢竟是學習的成果。
輸入不能為空的提示界面:
在US語言環境下,英文與中文輸入(但只有英文能正常朗讀):
?
? ? ? ? ? ? ??
?
總結
按照上述流程,英語(US)語言環境可以正常,其他四國語言未測試(按Google說明應該不成問題)。
資料顯示TTS暫時不支持中文,但是在設置語言環境時,寫入如下代碼:
1 int result = tts.setLanguage(Locale.CHINA);或:
1 int result = tts.setLanguage(Locale.CHINESE);奇怪的是工程并沒有報錯,而且運行后按鈕是使能狀態,說明通過了語言存在與支持的判斷環節。不過輸入中文,點擊之后是沒有任何反應的,當然,此時輸入什么都不會有反應。這是存在疑問的地方,希望知道的大神指點一二。
網上也提供了替代方案,比如利用第三方支持庫SVox等。
轉載于:https://www.cnblogs.com/tgyf/p/4733033.html
總結
- 上一篇: POJ C程序设计进阶 编程题#3:运
- 下一篇: viewDidLoad, viewWil