Android自动朗读(TTS)的实现

Android提供了自动朗读支持。可以对指定文本内容进行朗读,从而发生声音;还允许把文本对应的音频录制成音频文件,方便以后播放。Android的自动朗读主要通过TextToSpeech来完成,构造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);当创建TextToSpeech对象时,必须先提供一个OnInitListener监听器负责监听TextToSpeech的初始化结果。

使用TextToSpeech的步骤如下:

  • 创建TextToSpeech对象,创建时传入OnInitListener监听器监听示范创建成功
  • 设置TextToSpeech所使用语言国家选项,通过返回值判断TTS是否支持该语言、国家选项
  • 调用speak()或synthesizeToFile方法
  • 关闭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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class MainActivity 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_main);

DexterPermissionHelper.requestPermissions(this, new DexterPermissionHelper.PermissionsCheckListener() {
@Override
public void onPermissionsGranted(int permissionType) {

}

@Override
public void onPermissionsDenied(int permissionType) {

}
});

textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == textToSpeech.SUCCESS) {
//int result = textToSpeech.setLanguage(new Locale("th", "TH"));
int result = textToSpeech.setLanguage(Locale.CHINA);
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE){
Toast.makeText(MainActivity.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,
"/sdcard/ttsdemo/record_audio/sound.wav");
Toast.makeText(MainActivity.this, "声音记录成功", Toast.LENGTH_SHORT).show();
}
});
}

@Override
protected void onDestroy() {
if (textToSpeech != null)
textToSpeech.shutdown();
super.onDestroy();
}
}
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"
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"
android:text="大家好"
/>

<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>

这里我们使用的是中文,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,我们可以对指定文本内容进行朗读,从而发生声音;还允许把文本对应的音频录制成音频文件,保存到本地。

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :