关于android自带语音播报使用

关于android语音播报
private TextToSpeech textToSpeech = null;//创建自带语音对象

private void initTTS() {
//实例化自带语音对象
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == textToSpeech.SUCCESS) {
// Toast.makeText(MainActivity.this,“成功输出语音”,
// Toast.LENGTH_SHORT).show();
// Locale loc1=new Locale(“us”);
// Locale loc2=new Locale(“china”);

                textToSpeech.setPitch(0.5f);//方法用来控制音调
                textToSpeech.setSpeechRate(0.01f);//用来控制语速

                //判断是否支持下面两种语言
                int result1 = textToSpeech.setLanguage(Locale.US);
                int result2 = textToSpeech.setLanguage(Locale.
                        SIMPLIFIED_CHINESE);
                boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
                boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);

// Log.i(“zhh_tts”, “US支持否?–》” + a +
// “\nzh-CN支持否》–》” + b);
} else {
// MyToast.showToast(“数据丢失或不支持”);
// Toast.makeText(MainActivity.this, “数据丢失或不支持”, Toast.LENGTH_SHORT).show();
}

        }
    });
} private void initTTS() {
    //实例化自带语音对象
    textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == textToSpeech.SUCCESS) {
                // Toast.makeText(MainActivity.this,"成功输出语音",
                // Toast.LENGTH_SHORT).show();
                // Locale loc1=new Locale("us");
                // Locale loc2=new Locale("china");

                textToSpeech.setPitch(0.5f);//方法用来控制音调
                textToSpeech.setSpeechRate(0.01f);//用来控制语速

                //判断是否支持下面两种语言
                int result1 = textToSpeech.setLanguage(Locale.US);
                int result2 = textToSpeech.setLanguage(Locale.
                        SIMPLIFIED_CHINESE);
                boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
                boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);

// Log.i(“zhh_tts”, “US支持否?–》” + a +
// “\nzh-CN支持否》–》” + b);
} else {
// MyToast.showToast(“数据丢失或不支持”);
// Toast.makeText(MainActivity.this, “数据丢失或不支持”, Toast.LENGTH_SHORT).show();
}

        }
    });
}
private void startAuto(String data, boolean b) {//播放的函数方法
    // 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规

    textToSpeech.setPitch(1.f);
    Log.d("wpnqq", "startAuto: " + b);

    // 设置语速
    textToSpeech.setSpeechRate(8.01f);
    textToSpeech.speak(data,//输入中文,若不支持的设备则不会读出来
            TextToSpeech.QUEUE_FLUSH, null);
}
注意:本机在华为手机运行,本地语言要改成中文
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值