Android自带的TTS功能

在Android1.6之后添加了TextToSpeech,也叫TTS,把相应的文字转化成语音播报,增强了用户体验。可以根据语言播报


界面上的控件如下:

Android自带的TTS功能_第1张图片

可以选择的语言


但有的语言不支持,比如中文就不支持


代码也比较简单,能简单介绍TTS的用法

MainActivity.java

package com.example.tts;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * 根据选择的语言播报TTS
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends Activity {

	private TextToSpeech mSpeech = null;
	private Spinner langSpinner = null;
	private EditText edit = null;
	private Button btn = null;
	private String[] langs;
	private String curLang;
	private List<String> langList = new ArrayList<String>();
	private ArrayAdapter<String> langAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		langs = getResources().getStringArray(R.array.languages); // 得到语言数组
		langSpinner = (Spinner) findViewById(R.id.spinner);
		edit = (EditText) findViewById(R.id.edit);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new BtnListener());
		
		for (int i = 0; i < langs.length; i++) {
			langList.add(langs[i]);
		}
		// 设置下拉框的适配器和样式
		langAdapter = new ArrayAdapter<String>(MainActivity.this,
				android.R.layout.simple_spinner_item, langList);
		langAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		langSpinner.setAdapter(langAdapter);

		// 下拉框监听器
		langSpinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> adapter,
							View view, int position, long id) {
						// TODO Auto-generated method stub
						curLang = (String) langSpinner.getAdapter().getItem(
								(int) id);
						if(mSpeech != null)
						{
							mSpeech.stop();
							mSpeech.shutdown();
							mSpeech = null;
						}
						// 创建TTS对象
						mSpeech = new TextToSpeech(MainActivity.this, new TTSListener());
						Toast.makeText(MainActivity.this, "select = " + curLang, Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
	}

	private int SetLanguage(String lang) {
		int result = 0;
		if (lang.equals("CANADA")) {
			result = mSpeech.setLanguage(Locale.CANADA);
		} else if (lang.equals("CANADA_FRENCH")) {
			result = mSpeech.setLanguage(Locale.CANADA_FRENCH);
		} else if (lang.equals("CHINA")) {
			result = mSpeech.setLanguage(Locale.CHINA);
		} else if (lang.equals("CHINESE")) {
			result = mSpeech.setLanguage(Locale.CHINESE);
		} else if (lang.equals("ENGLISH")) {
			result = mSpeech.setLanguage(Locale.ENGLISH);
		} else if (lang.equals("FRANCE")) {
			result = mSpeech.setLanguage(Locale.FRANCE);
		} else if (lang.equals("FRENCH")) {
			result = mSpeech.setLanguage(Locale.FRENCH);
		} else if (lang.equals("GERMAN")) {
			result = mSpeech.setLanguage(Locale.GERMAN);
		} else if (lang.equals("GERMANY")) {
			result = mSpeech.setLanguage(Locale.GERMANY);
		} else if (lang.equals("ITALIAN")) {
			result = mSpeech.setLanguage(Locale.ITALIAN);
		} else if (lang.equals("ITALY")) {
			result = mSpeech.setLanguage(Locale.ITALY);
		} else if (lang.equals("JAPAN")) {
			result = mSpeech.setLanguage(Locale.JAPAN);
		} else if (lang.equals("JAPANESE")) {
			result = mSpeech.setLanguage(Locale.JAPANESE);
		} else if (lang.equals("KOREA")) {
			result = mSpeech.setLanguage(Locale.KOREA);
		} else if (lang.equals("KOREAN")) {
			result = mSpeech.setLanguage(Locale.KOREAN);
		} else if (lang.equals("PRC")) {
			result = mSpeech.setLanguage(Locale.PRC);
		} else if (lang.equals("ROOT")) {
			result = mSpeech.setLanguage(Locale.ROOT);
		} else if (lang.equals("SIMPLIFIED_CHINESE")) {
			result = mSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
		} else if (lang.equals("TAIWAN")) {
			result = mSpeech.setLanguage(Locale.TAIWAN);
		} else if (lang.equals("TRADITIONAL_CHINESE")) {
			result = mSpeech.setLanguage(Locale.TRADITIONAL_CHINESE);
		} else if (lang.equals("UK")) {
			result = mSpeech.setLanguage(Locale.UK);
		} else if (lang.equals("US")) {
			result = mSpeech.setLanguage(Locale.US);
		}
		return result;
	}

	private class TTSListener implements OnInitListener {

		@Override
		public void onInit(int status) {
			// TODO Auto-generated method stub
			if (status == TextToSpeech.SUCCESS) {
				//int result = mSpeech.setLanguage(Locale.ENGLISH);
				int result = SetLanguage(curLang);  
				//如果打印为-2,说明不支持这种语言
				Toast.makeText(MainActivity.this, "-------------result = " + result, Toast.LENGTH_LONG).show();
				if (result == TextToSpeech.LANG_MISSING_DATA
						|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
					System.out.println("-------------not use");
				} else {
					mSpeech.speak("i love you", TextToSpeech.QUEUE_FLUSH, null);
				}
			}
		}

	}

	private class BtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mSpeech.speak(edit.getText().toString(), TextToSpeech.QUEUE_FLUSH,
					null);
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (mSpeech != null) {
			mSpeech.stop();
			mSpeech.shutdown();
			mSpeech = null;
		}
		super.onDestroy();
	}

}

布局文件

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text"
        android:text="@string/select_language" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:layout_below="@id/label"
        android:drawSelectorOnTop="false" />

     <TextView
        android:id="@+id/label2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner"
        android:text="@string/text" />
     
     <EditText 
         android:id="@+id/edit"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/label2"
         android:text="@string/example"
         android:inputType="text"
         />
     
     <Button 
         android:id="@+id/btn"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/edit"
         android:text="@string/btn"
         />
</RelativeLayout>


代码下载:

点击打开链接







你可能感兴趣的:(android,Spinner,tts,TextToSpeech)