首先封装一个tts类,起名为TtsTest:代码如下:
package example.com.myapplication; import android.content.Context; import android.speech.tts.TextToSpeech; import android.util.Log; import java.util.HashMap; import java.util.Locale; import java.util.concurrent.ConcurrentLinkedQueue; /** * Created by 联想 on 2016/8/17. */ public class TtsTest implements TextToSpeech.OnInitListener { private final TextToSpeech mTextToSpeech;//TTS对象 private final ConcurrentLinkedQueue mBufferedMessages;//消息队列 private Context mContext; private boolean mIsReady;//标识符 public TtsTest(Context context){ this.mContext=context;//获取上下文 this.mBufferedMessages=new ConcurrentLinkedQueue();//实例化队列 this.mTextToSpeech=new TextToSpeech(this.mContext,this);//实例化TTS } //初始化TTS引擎 @Override public void onInit(int status) { Log.i("TextToSpeechDemo",String.valueOf(status)); if(status==TextToSpeech.SUCCESS){ int result = this.mTextToSpeech.setLanguage(Locale.US);//设置识别语音为英文或者中文 synchronized (this){ this.mIsReady=true;//设置标识符为true for(Object bufferedMessage : this.mBufferedMessages){ String s = String.valueOf(bufferedMessage); speakText(s);//读语音 } this.mBufferedMessages.clear();//读完后清空队列 } } } //释放资源 public void release(){ synchronized (this){ this.mTextToSpeech.shutdown(); this.mIsReady=false; } } //更新消息队列,或者读语音 public void notifyNewMessage(String lanaugh){ String message=lanaugh; synchronized (this){ if(this.mIsReady){ speakText(message); }else{ this.mBufferedMessages.add(message); } } } //读语音处理 private void speakText(String message){ Log.i("liyuanjinglyj",message); HashMap params=new HashMap(); params.put(TextToSpeech.Engine.KEY_PARAM_STREAM,"STREAM_NOTIFICATION");//设置播放类型(音频流类型) this.mTextToSpeech.speak(message, TextToSpeech.QUEUE_ADD, params);//将这个发音任务添加当前任务之后 this.mTextToSpeech.playSilence(100,TextToSpeech.QUEUE_ADD,params);//间隔多长时间 } }
然后再在
MainActivity中进行使用。
mainActivity中的代买如下::package example.com.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { TtsTest tts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tts = new TtsTest(this); final EditText edtText = (EditText) findViewById(R.id.edtText); Button btnRead = (Button) findViewById(R.id.btnRead); btnRead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { tts.notifyNewMessage(edtText.getText().toString()); } }); } @Override protected void onDestroy() { super.onDestroy(); tts.release(); } }
activity_main布局文件代码如下::<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="example.com.myapplication.MainActivity"> <EditText android:id="@+id/edtText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入需要朗读的文字" /> <Button android:id="@+id/btnRead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击朗读"/> </LinearLayout>