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>

最后截图来一张
Android TTS的简单应用_第1张图片

更多相关文章

  1. android 使内容铺满全屏
  2. Android App怎样调用 Frameworks Bluetooth接口
  3. Android Dialog 去除背景内容模糊
  4. android中如何在代码中直接设置View的layout_weight属性
  5. Android震动代码解读
  6. android 学习笔记有用代码片段(2)
  7. android 关于gallery 的平滑代码滚动
  8. android获取联系人所有内容
  9. Android客户端自动更新代码

随机推荐

  1. Yii授权之基于角色的存取控制 (RBAC)
  2. python怎么添加环境变量
  3. PHP面试踩过的坑
  4. php响应Json字符串头部出现非法字符“\u
  5. 几个不常用但特别实用的PHP预定义变量
  6. php架构师主要是做什么的
  7. php实现对图片对称加解密(适用身份证加密
  8. php开发环境是什么
  9. PHP源码—implode函数源码分析
  10. PHP基础之输出缓冲区基本概念、原理分析