java tts 中文跨平台_android TTS 语音,利用系统自带的API来语音提示,不支持中文...

package com.example.androidttsdemothird;

import java.util.HashMap;

import java.util.Locale;

import java.util.StringTokenizer;

import android.os.Bundle;

import android.app.Activity;

import android.content.SharedPreferences;

import android.speech.tts.TextToSpeech;

import android.speech.tts.TextToSpeech.OnInitListener;

import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity implements OnInitListener,

OnUtteranceCompletedListener {

private EditText inputText = null;

private Button speakBtn = null;

private TextToSpeech mTts;

private static final String TAG = "TTS Demo";

private static final String STORE_NAME = "preferenceFile";

private static final String loveConfession = "你好, you had me at hello. To the world you maybe one person, but to one person you maybe the world. Please believe me, I was prepared for everything,except you. I love that you are the last person I want to talk to before I go to sleep at night. Love means never having to say you're sorry. So choose me. Marry me. Let me make you happy. ";

private String[] loveArray;

private int lastUtterance = -1;

private HashMap params = new HashMap();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 创建TextToSpeech实例,初始化完成后会调用OnInitListener(第二个参数)的回调函数

mTts = new TextToSpeech(this, this // TextToSpeech.OnInitListener

);

//设置控件

inputText = (EditText)findViewById(R.id.inputText);

speakBtn = (Button)findViewById(R.id.speakBtn);

inputText.setText(loveConfession);

speakBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

//处理输入框里的内容

StringTokenizer loveTokens = new StringTokenizer(inputText.getText().toString(),",.");

int i = 0;

loveArray = new String[loveTokens.countTokens()];

while(loveTokens.hasMoreTokens())

{

loveArray[i++] = loveTokens.nextToken();

}

//朗读输入框里的内容

speakText();

}

});

}

private void speakText() {

lastUtterance++;

if(lastUtterance >= loveArray.length)

{

lastUtterance = 0;

}

Log.v(TAG, "the begin utterance is " + lastUtterance);

for(int i = lastUtterance; i < loveArray.length; i++)

{

//为每个语音片段都设置一个标记

params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(i));

mTts.speak(loveArray[i], TextToSpeech.QUEUE_ADD, params);

}

}

@Override

public void onInit(int status) {

// TODO Auto-generated method stub

//TTS Engine初始化完成

if(status == TextToSpeech.SUCCESS)

{

int result = mTts.setLanguage(Locale.US);

//设置发音语言

if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)

//判断语言是否可用

{

Log.v(TAG, "Language is not available");

speakBtn.setEnabled(false);

}

else

{

speakBtn.setEnabled(true);

//设置一个语音片段结束后的回调函数

mTts.setOnUtteranceCompletedListener(this);

}

}

}

@Override

public void onUtteranceCompleted(String utteranceId) {

// TODO Auto-generated method stub

//一个语音片段结束后的回调函数

Log.v(TAG, "Get completed message for the utteranceId " + utteranceId);

//用lastUtterance记录当前结束的语音片段

lastUtterance = Integer.parseInt(utteranceId);

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

//释放TTS的资源

if(mTts != null)

{

mTts.stop();

mTts.shutdown();

}

//保存lastUtterance值

SharedPreferences settings = getSharedPreferences(STORE_NAME, 0);

SharedPreferences.Editor editor = settings.edit();

editor.putInt("lastUtterance", lastUtterance);

editor.commit();

Log.v(TAG, "the stored lastUtterance is " + lastUtterance);

super.onDestroy();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值