Google自带语音播放功能实现

来源:互联网 发布:数学试卷编辑软件 编辑:程序博客网 时间:2024/05/18 13:28

 

就是播放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 初始化方法内判断语音加载是否成功 确实很简单了

就是不知道什么时候可以支持中文啊。

原创粉丝点击