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,回收资源。
布局⽂件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android/apk/res/android"
xmlns:app="schemas.android/apk/res-auto"
xmlns:tools="schemas.android/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布局
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⽂件
public class SpeechActivity extends AppCompatActivity {
private EditText input;
private Button speech,record;
private TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle 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(d);
speech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textToSpeech.Text().toString(),
TextToSpeech.QUEUE_ADD, null);
}
});
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String inputText = Text().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();
}
}
这⾥我们使⽤的是中⽂,int result = textToSpeech.setLanguage(Locale.CHINA);你也可以根据⾃⼰的需求更改为其他⽀持的语⾔。
最后在l中加⼊权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
总结:通过使⽤Android提供的TTS,我们可以对指定⽂本内容进⾏朗读,从⽽发⽣声⾳;还允许把⽂本对应的⾳频录制成⾳频⽂件,保存到本地。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。