Android TTS的简单应用

MIanActivity的代码

package com.ttsactivity;import java.util.Locale;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;//先导入在实现接口,在接口直接输入TextToSpeech;public class MainActivity extends Activity implements TextToSpeech.OnInitListener,OnClickListener{    private TextToSpeech tts;    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tts = new TextToSpeech(this, this);        Button button = (Button) findViewById(R.id.button);        textView = (TextView) findViewById(R.id.textview);        button.setOnClickListener(this);    }    //继承接口实现的方法    @Override    public void onInit(int status) {        if(status == TextToSpeech.SUCCESS){            int result = tts.setLanguage(Locale.US);            if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){                Toast.makeText(this, "语言不存在", Toast.LENGTH_SHORT).show();            }        }    }    //读取内容    @Override    public void onClick(View view) {        tts.speak(textView.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);    }}

布局文件的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/text" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="朗读文本" />LinearLayout>

资源文件的代码,读的内容所在地

最重要的是第三局,那是我要读的内容

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TTSActivitystring>    <string name="action_settings">Settingsstring>    <string name="text">to the world you mabe one person,but to one person you mabe the worldstring>resources>

最后截图来一张

更多相关文章

  1. Error:(16, 0) Gradle DSL method not found: 'android()'
  2. Android(安卓)Dialog 去除背景内容模糊
  3. 3.Retrofit上传头像给后端
  4. android截屏示例,适合流媒体 MediaPlayer截屏
  5. android 使内容铺满全屏
  6. Android(安卓)Java代码执行adb Shell命令
  7. Android震动代码解读
  8. Android隐藏ListView的滚动条
  9. Android(安卓)App怎样调用 Frameworks Bluetooth接口

随机推荐

  1. centos系统安装redis4.0(单机)
  2. 对linux字符设备的理解(整体架构)
  3. 【嵌入式Linux学习七步曲之第五篇 Linux
  4. 如何查看最近安装的rpms?
  5. Uncompressing Linux... done, booting t
  6. 小米随身WiFi,Linux下AP热点驱动(开源)
  7. 走近Linux世界:Linux与Windows特性比较
  8. linux系统时间和硬件时钟问题(date和hwcl
  9. Linux 下串口编程之三 termios结构体介绍
  10. Linux上配置使用iSCSI详细说明