近期项目中用到语音转文字,原本打算使用第三方sdk,后来发现安卓原生就提供了一个类处理这个事务。TextToSpeech无需集成,使用起来也更简单,在这里记录一下
TextToSpeech ,这个控件的好处是朗读文字不需要任何的权限,其次不用联网避免了如讯飞的联网登权限(讯飞其实也不错,比较智能,缺点是该控件只能读取简单的文字)
使用:
布局:
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="需要播报的文字" />
<Button
android:id="@+id/button_read"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="转换语音"/>
activity代码:
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private EditText edText; //朗读的文字
p