Google自帶語音播放功能實現

 

就是播放EditText中的內容

Acitivity中

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.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
public class VoisePlayDemo extends Activity {  
    private TextToSpeech mSpeech;  
    private Button btn;  
    private EditText mEditText;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btn = (Button) findViewById(R.id.Button01);  
        mEditText = (EditText) findViewById(R.id.EditText01);  
        btn.setEnabled(false);  
        //創建TextToSpeech對象    
        mSpeech = new TextToSpeech(this, new OnInitListener() {  
            @Override  
            public void onInit(int status) {  
                if (status == TextToSpeech.SUCCESS) {  
                    int result = mSpeech.setLanguage(Locale.US);  
                    if (result == TextToSpeech.LANG_MISSING_DATA  
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {  
                        Log.e("bb", "not use");  
                    } else {  
                        btn.setEnabled(true);  
                    }  
                }  
            }  
        });  
        btn.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                mSpeech.speak(mEditText.getText().toString(),  
                        TextToSpeech.QUEUE_FLUSH, null);  
            }  
        });  
    }  
    @Override  
    protected void onDestroy() {  
        if (mSpeech != null) {  
            mSpeech.stop();  
            mSpeech.shutdown();  
        }  
        super.onDestroy();  
    } 
通過創建TextToSpeech類的實例 並在 onInit 初始化方法內判斷語音加載是否成功 確實很簡單了

就是不知道什麼時候可以支持中文啊。

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