日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文本 To 音频

發(fā)布時(shí)間:2025/4/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文本 To 音频 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文本 ?To ?音頻

?

TextToSpeech介紹

  TextToSpeech,簡稱 TTS,是Android 1.6版本中比較重要的新功能。將所指定的文本轉(zhuǎn)成不同語言音頻輸出。它可以方便的嵌入到游戲或者應(yīng)用程序中,增強(qiáng)用戶體驗(yàn)。

  TTS Engine,依托于當(dāng)前Android Platform所支持的幾種主要語言:English、French、German、Italian及Spanish五大語言(暫時(shí)沒有直接中文)。TTS可以將文本隨意的轉(zhuǎn)換成以上任意五種語言的語音輸出。

????????

布局設(shè)計(jì)

  界面中組件的設(shè)計(jì)很簡單,分為四個(gè)部分:

  1、內(nèi)容編輯框,供用戶輸入需要轉(zhuǎn)換成語音的文本;

  2、音頻播放按鈕,點(diǎn)擊后播放文本對(duì)應(yīng)的音頻;

  3、語調(diào)編輯框,供用戶調(diào)節(jié)朗讀的語調(diào),支持浮點(diǎn)型數(shù)據(jù),默認(rèn)值為1.0;

  4、語速編輯框,供用戶調(diào)節(jié)語速的語速,只能為整型數(shù)據(jù),默認(rèn)值為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應(yīng)用

  1、在應(yīng)用程序中實(shí)現(xiàn)TTS的TextToSpeech.OnInitListener監(jiān)聽器,測(cè)試案例針對(duì)onInit(int status)和onDestroy()兩個(gè)方法進(jìn)行了重載。代碼如下:

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、對(duì)TTS、Button及EditText組件的對(duì)象進(jìn)行初始化,是在主類MainActivity的重載方法onCreate(Bundle savedInstanceState) 中實(shí)現(xiàn),代碼如下:

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對(duì)象tts時(shí)會(huì)對(duì)上面定義的onInit(int status)方法進(jìn)行調(diào)用,這個(gè)過程非常關(guān)鍵。本案例中讓其完成的事情包括:

  A、判斷初始化狀態(tài);

  B、若成功,設(shè)置文本轉(zhuǎn)換語言環(huán)境;

  C、若語言環(huán)境存在并支持,則繼續(xù).....

  3、點(diǎn)擊音頻播放按鈕后,響應(yīng)方法為ToSpeak(),其實(shí)現(xiàn)如下:

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 }

  這里的代碼就好理解了,主要是在獲取用戶輸入的文本、語調(diào)及語速之后,進(jìn)行音頻的播放。注意:方法開頭對(duì)文本編輯框中的內(nèi)容進(jìn)行了空字串(“” )判斷,若為空則沒有播放的必要。

?

結(jié)果圖

  雖然界面很簡陋,還是附上幾張圖片把,畢竟是學(xué)習(xí)的成果。

  輸入不能為空的提示界面:

  在US語言環(huán)境下,英文與中文輸入(但只有英文能正常朗讀):

?

? ? ? ? ? ?                  ??  

?

總結(jié)

  按照上述流程,英語(US)語言環(huán)境可以正常,其他四國語言未測(cè)試(按Google說明應(yīng)該不成問題)。

  資料顯示TTS暫時(shí)不支持中文,但是在設(shè)置語言環(huán)境時(shí),寫入如下代碼:

1 int result = tts.setLanguage(Locale.CHINA);

  或:

1 int result = tts.setLanguage(Locale.CHINESE);

  奇怪的是工程并沒有報(bào)錯(cuò),而且運(yùn)行后按鈕是使能狀態(tài),說明通過了語言存在與支持的判斷環(huán)節(jié)。不過輸入中文,點(diǎn)擊之后是沒有任何反應(yīng)的,當(dāng)然,此時(shí)輸入什么都不會(huì)有反應(yīng)。這是存在疑問的地方,希望知道的大神指點(diǎn)一二。

  網(wǎng)上也提供了替代方案,比如利用第三方支持庫SVox等。

轉(zhuǎn)載于:https://www.cnblogs.com/tgyf/p/4733033.html

總結(jié)

以上是生活随笔為你收集整理的文本 To 音频的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。