TextToSpeech之阅读文字

创建阅读类

/**
 * Created by RongGuang on 2014-11-21.
 * 中文朗读
 */
public class ChineseToSpeech {
    private TextToSpeech textToSpeech;

    public ChineseToSpeech() {
        this.textToSpeech = new TextToSpeech(Application.getContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        new CoolToast(Application.getContext()).show("不支持朗读功能");
                    }
                }
            }
        });
    }

    public void speech(String text) {
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void destroy() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
    }
}

在使用的时候直接调用相应的方法即可实现阅读。

你可能感兴趣的:(TextToSpeech之阅读文字)