借助“手说”免费应用软件,实现中文朗读(Android TTS实践)

目前android是支持英文朗读的,但不支持中文朗读,这意味着如果想在自己的应用中实现中文朗读的话,那必须依靠第三方软件(针对菜鸟而言,大神自己都可以开发中文朗读)。

笔者借助“手说”软件,开发了一个小demo,可以朗读用户输入的中英文文本。中文朗读可以读出英文字母,但不能读成单词,而英文朗读则只能读出英文文本。

以下是android工程的说明:

先看个效果图:


界面非常简单.

新建Android工程后,只需要三步:

1.在libs文件夹中添加手说TTS提供的jar包,这个可以从http://shoushuo.com/index.html上找到并下载。

2.修改xml文件,也就是实现效果图的界面,这个非常简单,代码如下:

<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:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity" 
    android:weightSum="1">

    <EditText
        android:id="@+id/inputwords"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"
        android:textSize="16pt"
        android:hint="请输入想要朗读的内容……" />
    
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="0.1"
    >
    <Button
        android:id="@+id/speakinchinese"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="中文"
        android:clickable="true"
        android:onClick="chinese"
        android:layout_weight="1"
       />
    <Button
        android:id="@+id/empty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="清空"
        android:clickable="true"
        android:onClick="empty"
        android:layout_weight="1"
       />
	<Button
        android:id="@+id/speakinenglish"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="English"
        android:clickable="true"
        android:onClick="english"
        android:layout_weight="1"
       />
	
     </LinearLayout>
</LinearLayout>
3.自然是修改java主程序了,代码如下:

import java.util.Locale;
import com.shoushuo.android.tts.ITts;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.speech.tts.TextToSpeech.OnInitListener;

public class MainActivity extends Activity implements OnInitListener {
	private TextView inputWordsTextView;
	private TextToSpeech ttsInEnglish;// English语音播报控件

	// 中文朗读,手说,移植代码块开始
	private ITts ttsInChinese; // 中文语音播报控件
	private boolean ttsBound;

	/**
	 * tts服务连接.
	 */
	private final ServiceConnection ttsConnection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			ttsInChinese = null;
			ttsBound = false;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			ttsInChinese = ITts.Stub.asInterface(service);
			ttsBound = true;
			try {
				// tts服务初始化
				ttsInChinese.initialize();
			} catch (RemoteException e) {
			}
		}
	};

	// 移植代码块结束

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inputWordsTextView = (TextView) findViewById(R.id.inputwords);
		ttsInEnglish = new TextToSpeech(this, this);// English语音播报

	}

	// 移植代码块开始
	@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);
		}
	}

	// 移植代码块完

	// 点击"中文"按钮
	public void chinese(View view) {
		readInChinese(inputWordsTextView.getText().toString());
	}

	public void readInChinese(String readText) {
		 try {
			ttsInChinese.speak(readText, TextToSpeech.QUEUE_FLUSH);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	// 点击"English"按钮
	public void english(View view) {
		readInEnglish(inputWordsTextView.getText().toString());
	}

	public void readInEnglish(String readText) {
		ttsInEnglish.speak(readText, TextToSpeech.QUEUE_FLUSH, null);
	}

	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = ttsInEnglish.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Toast.makeText(this, "Language is not available",
						Toast.LENGTH_LONG).show();
			}
		}
	}

	// 点击"清空"按钮
	public void empty(View view) {
		inputWordsTextView.setText("");
	}

}

最后说明一下,这个小应用必须具备的一个条件就是要同时安装“手说”应用,各大安卓应用市场都可以找到,否则点击“中文”的时候应用会因找不到“手说”而退出。另外,详细的开发也可以参考http://shoushuo.com/sstts.html

以上内容仅供学习交流,请勿将“手说”应用于商业用途。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值