TextToSpeech自动朗读使用


/*
自动朗读TTS(Text To Speech)
    使用4步:
        1 new TextToSpeech()对象,需要一个监听器参数(监听TextToSpeech对象初始化状态,实现ini方法,初始完对象回调)
        2 设置语言(不支持中文) setLanguage() 如Locale.UK
        3 speak()方法朗读 有模式新朗读/追加
        4 shutdown()关闭TextToSpeech

*/

public class MainActivity extends Activity {

    TextToSpeech textToSpeech;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getContentResolver().registerContentObserver();
        editText = (EditText) findViewById(R.id.editText);
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    //textToSpeech.getAvailableLanguages();
                    int result = textToSpeech.setLanguage(Locale.ENGLISH);

                    if (result != TextToSpeech.LANG_AVAILABLE && result != TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                        Toast.makeText(MainActivity.this, "不支持该语言", Toast.LENGTH_SHORT).show();
                    }
                }


            }

        });

    }

    public void write(View view) {

        textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
//待转换文本 播放模式 新任务/追加 音频转换时参数
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (textToSpeech != null)
            textToSpeech.shutdown();

    }

    public void record(View view) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                String path = Environment.getExternalStorageDirectory().getCanonicalPath();
                textToSpeech.synthesizeToFile(editText.getText().toString(), null, path + "/mysound.wav");
                Toast.makeText(MainActivity.this, "save Successful!", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋葵好吃吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值