android实现文本朗读,TextToSpeech

  搞android开发的同学,在很多时候,尤其是在做和语音相关的开发时都会遇到要将文本转为语音输出的问题,没有接触过这方面的同学可能会很头大。其实要实现这个功能是很简单的,我来为大家讲解。

  首先,实现文字转语音使用到android自带的TextToSpeech类,可喜的是这个类在通用的android基本包里,这样我们就没有必要四处寻找第三方的JAR报了。

import android.speech.tts.TextToSpeech;

private TextToSpeech tts;

   其次,要想实例化这个对象需要两个参数,一个是Context对象,另一个是TextToSpeech类对应的监听器对象:OnLnitListener对象。一般Context对象传入当前的Activity,OnLnitListener可以自己写类继承,并实现其方法。

import android.speech.tts.TextToSpeech.OnInitListener;

tts = new TextToSpeech(this, OnInitListener);

   OnLnitListener接口中只要是onInit方法,其功能是对tts对象进行初始化,设置一下语言,判断文字是否转换成功以及当前系统是否支持该语言。

	@Override
	public void onInit(int status){
		// 判断是否转化成功
		if (status == TextToSpeech.SUCCESS)
		{
			Toast.makeText(MainActivity.this, "成功输出语音", Toast.LENGTH_SHORT).show();
			//指定当前朗读的是英文,如果不是给予提示
//			int result = tts.setLanguage(Locale.US);
			int result = tts.setLanguage(Locale.CHINESE);//制定当前阅读的是中文
			if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
			{
				Toast.makeText(MainActivity.this, R.string.notAvailable, Toast.LENGTH_SHORT).show();
			}
		}
	}

   最后,只要在合适的时候调用tts转文字到语音的方法即可。

tts.speak("要转化的文字,if don't support chinese,please input english", TextToSpeech.QUEUE_FLUSH, null);

下面就是我开发的一个小案例,供大家参考。

package com.example.speechtest;

import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnInitListener{
	private Button speechButton;
	private TextView speechText;
	private TextToSpeech tts;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tts = new TextToSpeech(this, this);
        
        speechText = (TextView)findViewById(R.id.speechTextView);
        speechButton = (Button)findViewById(R.id.speechButton);
        speechButton.setOnClickListener(new OnClickListener()
		{
			
			@SuppressLint("ShowToast")
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				tts.speak(speechText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
			}
		});
    }
	@Override
	public void onInit(int status)
	{
		// TODO Auto-generated method stub
		if (status == TextToSpeech.SUCCESS)
		{
			Toast.makeText(MainActivity.this, "成功输出语音", Toast.LENGTH_SHORT).show();
			//指定当前朗读的是英文,如果不是给予提示
//			int result = tts.setLanguage(Locale.US);
			int result = tts.setLanguage(Locale.CHINESE);//制定当前阅读的是中文
			if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
			{
				Toast.makeText(MainActivity.this, R.string.notAvailable, Toast.LENGTH_SHORT).show();
			}
		}
	}
}



 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
	android:id="@+id/speechButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/speech"
    />
<TextView
	android:id="@+id/speechTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/text"
    />
</LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SpeechTest</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    
     <string name="speech">朗读</string>
	<string name="notAvailable">Language is not available.</string>
    <string name="text">
        你好,我的位置是岳麓区长沙市;\n
        My Email is: [email protected].\n
		end \n
	</string>

</resources>

猜你喜欢

转载自hafuokas.iteye.com/blog/1943899