android 语音播报中文,android 语音播报(通过手说tts 实现中文语音播报)

本文介绍了如何在Android平台上使用手说TTS(Text To Speech)引擎进行中文文本到语音的转换。通过实例代码展示了如何集成手说TTS的jar库,实现文本输入后进行语音朗读的功能,包括处理中文多音字和音调转换。开发者可以参考这些步骤在自己的应用程序中实现中文语音输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手说TTS介绍:

手说TTS,是Android平台下的中文语音引擎,提供了中文文本到语音的转换。

使用手说TTS进行中文文本的朗读,包括中文简繁体、阿拉伯数字、英文字母及一些符号的混读。并且处理了中文的多音字和音调转换等问题。

开发人员可以使用手说TTS来开发Android平台下需要中文语音的应用程序。

实例代码开发准备:

第一步:安装手说TTS安装包

安装到真实手机或者手机模拟器中。(如果不安装shoushouTTS.apk文件,就进行功能代码编写肯定会报错,记住:第一步先安装shuoshouTTS.apk)

第二步:下载手说TTS客户类库包

将该jar文件引入到你的应用中。

废话不多说,直接上源码:

布局文件:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/edtSpeectText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="太阳从东边升起,慢慢的露出红彤彤的笑脸。" />

android:id="@+id/btnSpeechGo"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="speechText"

android:text="开始朗读" />

核心功能代码文件:

package com.shoushuo.voicebroadcast;

import com.shoushuo.android.tts.ITts;

import com.shoushuo.android.tts.ITtsCallback;

import android.os.Bundle;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.os.RemoteException;

import android.speech.tts.TextToSpeech;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

public class MainActivity extends Activity {

private EditText edtSpeectText;

private Button btnSpeechGo;

private Context context;

private ITts ttsService;

private boolean ttsBound;

/**

* 定义Handler.

*/

private final Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

Toast.makeText(context, " 我的话说完了 ", Toast.LENGTH_SHORT).show();

btnSpeechGo.setEnabled(true);

}

};

/**

* 回调参数.

*/

private final ITtsCallback ttsCallback = new ITtsCallback.Stub() {

// 朗读完毕.

@Override

public void speakCompleted() throws RemoteException {

handler.sendEmptyMessage(0);

}

};

/**

* tts服务连接.

*/

private final ServiceConnection ttsConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName arg0) {

try {

// 注册回调参数

ttsService.unregisterCallback(ttsCallback);

} catch (RemoteException e) {

e.printStackTrace();

}

ttsService = null;

ttsBound = false;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

ttsService = ITts.Stub.asInterface(service);

ttsBound = true;

try {

// tts服务初始化

ttsService.initialize();

// 撤销回调参数.

ttsService.registerCallback(ttsCallback);

} catch (RemoteException e) {

}

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

context = this;

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

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

btnSpeechGo.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

speechText();

}

});

}

/**

* 按钮:朗读.

*

* @param v

*/

public void speechText() {

try {

ttsService.speak(edtSpeectText.getText().toString(),

TextToSpeech.QUEUE_FLUSH);

} catch (RemoteException e) {

e.printStackTrace();

}

}

@Override

protected void onDestroy() {

if (ttsBound) {

ttsBound = false;

// 撤销tts服务

this.unbindService(ttsConnection);

}

super.onDestroy();

}

@Override

protected void onStart() {

super.onStart();

if (!ttsBound) {

String actionName = "com.shoushuo.android.tts.intent.action.InvokeTts";

Intent intent = new Intent(actionName);

// 绑定tts服务

this.bindService(intent, ttsConnection, Context.BIND_AUTO_CREATE);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值