藉助“手說”免費應用軟件,實現中文朗讀(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

以上內容僅供學習交流,請勿將“手說”應用於商業用途。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章