Android 系统原生TTS使用

1.相关权限策略

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.TTS播放对象创建



import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.widget.Toast;

import java.util.Locale;

public class SystemTTS implements TextToSpeech.OnUtteranceCompletedListener{
    private Context mContext;
    private static SystemTTS mSystemTTS;
    private TextToSpeech mTextToSpeech;

    public static SystemTTS getInstance(Context context) {
        if (mSystemTTS == null) {
            synchronized (SystemTTS.class) {
                if (mSystemTTS == null) {
                    mSystemTTS = new SystemTTS(context);
                }
            }
        }
        return mSystemTTS;
    }

    private SystemTTS(Context context) {
        mContext = context.getApplicationContext();
        mTextToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTextToSpeech.setLanguage(Locale.US);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(mContext, "TTS不支持", Toast.LENGTH_SHORT).show();
                    }else{
                        mTextToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
                        mTextToSpeech.setSpeechRate(1.0f);
                        mTextToSpeech.setOnUtteranceProgressListener(new TTSUtteranceListener());
                        mTextToSpeech.setOnUtteranceCompletedListener(SystemTTS.this);//已经过时
                    }
                }
            }
        });
    }

    //此方法已经过时
    @Override
    public void onUtteranceCompleted(String utteranceId) {
    }

    private class TTSUtteranceListener extends UtteranceProgressListener {
        @Override
        public void onStart(String utteranceId) {
        }

        @Override
        public void onDone(String utteranceId) {
            if(mISpeechComplete != null){
                mISpeechComplete.speechComplete();
            }
        }
        @Override
        public void onError(String utteranceId) {
            if(mISpeechComplete != null){
                mISpeechComplete.speechError();
            }
        }
    }

    public void playText(String playText) {
        if (mTextToSpeech != null) {
            //依次播放MSG,不会打断当前播放内容
            mTextToSpeech.speak(playText,TextToSpeech.QUEUE_ADD,null,"UniqueID");//注意设置ID
            //依次播放MSG,如果有新的播放内容时,则取消上一次正在播放的内容,播放现在内容
            //mTextToSpeech.speak(playText,TextToSpeech.QUEUE_FLUSH,null,"UniqueID");
        }
    }

    public void stopSpeak() {
        if (mTextToSpeech != null) {
            mTextToSpeech.stop();
        }
    }

    private ISpeechComplete mISpeechComplete;
    public void setSpeechComplete(ISpeechComplete speechComplete){
        mISpeechComplete = speechComplete;
    }
    public interface ISpeechComplete{
        void speechComplete();
        void speechError();
    }
}

3.Activity全局使用

 SystemTTS mSystemTTS = SystemTTS.getInstance(this);
 mSystemTTS.playText("");

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值