android 语音提醒,文字转语音播放

android本身具有文字转语音播放的TTS,一些第三方的sdk,像百度语音,科大讯飞语音等等都提供了文字转语音播放的功能,但这些第三方sdk大都收费,还挺贵的。
这里推荐两种方法:1、android自带的TTS,适合播放量比较大的文本。部分手机不支持中文播放。声音比较难听
2、MediaPlayer播放语音,就类似支付宝语音到账的提醒等,不适合播放大量的文本,声音可以定制,可以由声优的录音播放。

下面介绍一下两种播放的代码。 代码地址

第一种播放方式 TTS

public class MyTTS extends UtteranceProgressListener {
    private  TextToSpeech tts;
    private boolean isSupportCN = true;
    private static MyTTS instance;

    public static MyTTS getInstance(Context context){
        if(instance == null){
            synchronized(MyTTS.class){
                instance = new MyTTS(context.getApplicationContext());
            }
        }
        return instance;
    }

    private MyTTS(final Context context){
        tts =  new TextToSpeech(context , new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status==TextToSpeech.SUCCESS){
                    int result = tts.setLanguage(Locale.CHINA);
                    tts.setPitch(1.0f);
                    tts.setSpeechRate(1.0f);
                    tts.setOnUtteranceProgressListener(MyTTS.this);
                    if(result == TextToSpeech.LANG_NOT_SUPPORTED || result ==TextToSpeech.LANG_MISSING_DATA){
                        isSupportCN = false;
                        Toast.makeText(context,"抱歉,不支持中文播放",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public void onStart(String utteranceId) {
        Log.d("xulc","onStart---utteranceId--->"+utteranceId);
    }

    @Override
    public void onDone(String utteranceId) {
        Log.d("xulc","onDone---utteranceId--->"+utteranceId);
    }

    @Override
    public void onError(String utteranceId) {
        Log.d("xulc","onError---utteranceId--->"+utteranceId);
    }


    public TextToSpeech getTts() {
        return tts;
    }

    public boolean isSupportCN() {
        return isSupportCN;
    }

}

第二种 Mediaplayer播放的方法

public class VoicePlayer {
    private ConcurrentLinkedQueue voiceUnitList;
    private int index = 0;   //播放的下标位置
    private MediaPlayer mediaPlayer;
    private Context mcontext;
    private boolean isPlaying = false;

    private static VoicePlayer instance;
    public static VoicePlayer getInstance(Context context){
        if(instance==null){
            synchronized (VoicePlayer.class){
                instance = new VoicePlayer(context.getApplicationContext());
            }
        }
        return instance;
    }

    private VoicePlayer(Context context){
        voiceUnitList = new ConcurrentLinkedQueue<>();
        mcontext = context;
    }

    public void addVoiceUnit(VoiceUnit unit){
        voiceUnitList.add(unit);
    }

    public void play(){   //调动播放
        if(isPlaying){  //正在播放

        }else{
            isPlaying = true;
            playVoice();
        }
    }


    private void playVoice(){
        if(mediaPlayer==null){
            mediaPlayer = new MediaPlayer();
        }
        VoiceUnit voiceUnit = voiceUnitList.peek();
        if(voiceUnit!=null) {     //内容不为空,播放当前信息
            if (index < voiceUnit.getVoices().length) {   //当前信息未播放完成。播放下一条
                final Uri uri = Uri.parse("android.resource://"+mcontext.getApplicationContext().getPackageName()+"/" +voiceUnitList.peek().getVoices()[index]);
                try {
                    mediaPlayer.setDataSource(mcontext, uri);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            Log.d("xulc", "播放了一个音频(index = ):"+index+"   "+uri.getPath());
                            mediaPlayer.reset();
                            index++;
                            playVoice();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{  //播放下一项
                Log.d("xulvcheng","播放下一项....");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        voiceUnitList.poll();
                        index = 0;
                        playVoice();
                    }
                },2000);       //确定是否会造成  内存泄漏
                //200ms 读取下一条
            }
        }else{
            isPlaying = false;  //播放完成
            index = 0;
            Log.d("xulvcheng","播放完成的状态....");
        }
    }

    private Handler handler = new MyHnadler();



    private static class MyHnadler extends Handler {}

    public void destory(){
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        if(handler!=null){
            handler.removeCallbacksAndMessages(null);
        }
    }
}

你可能感兴趣的:(android)