This commit is contained in:
wangcongtao
2020-04-14 17:08:24 +08:00
parent 7ed495aa9d
commit 2b415e499f
6 changed files with 96 additions and 13 deletions

View File

@@ -0,0 +1,80 @@
package com.mogo.utils.tts;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import java.util.Locale;
/**
* @author congtaowang
* @since 2020-04-14
* <p>
* 描述
*/
public class AndroidTTSPlayer extends UtteranceProgressListener implements TTSPlayer, TextToSpeech.OnInitListener {
private TextToSpeech mTtsEngine;
private Context mContext;
private boolean mIsSuccess = false;
private Bundle mParams;
/**
* {@link TextToSpeech.Engine#KEY_PARAM_STREAM},
* {@link TextToSpeech.Engine#KEY_PARAM_VOLUME},
* {@link TextToSpeech.Engine#KEY_PARAM_PAN}.
*
* @param context
*/
public AndroidTTSPlayer( Context context ) {
mContext = context.getApplicationContext();
mTtsEngine = new TextToSpeech( mContext, this );
mParams = new Bundle();
mParams.putInt( TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_ALARM );
mParams.putInt( TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_ALARM );
}
@Override
public void stop() {
if ( mTtsEngine != null ) {
mTtsEngine.stop();
}
}
@Override
public void speakTTS( String text ) {
// mTtsEngine.speak()
}
@Override
public void onInit( int status ) {
if ( status == TextToSpeech.SUCCESS ) {
int result = mTtsEngine.setLanguage( Locale.CHINA );
mTtsEngine.setPitch( 1.0f );// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
mTtsEngine.setSpeechRate( 1.0f );
mTtsEngine.setOnUtteranceProgressListener( this );
if ( result != TextToSpeech.LANG_MISSING_DATA || result != TextToSpeech.LANG_NOT_SUPPORTED ) {
//系统支持中文播报
mIsSuccess = true;
}
}
}
@Override
public void onStart( String utteranceId ) {
}
@Override
public void onDone( String utteranceId ) {
}
@Override
public void onError( String utteranceId ) {
}
}

View File

@@ -0,0 +1,14 @@
package com.mogo.utils.tts;
/**
* @author congtaowang
* @since 2020-04-14
* <p>
* 描述
*/
public interface TTSPlayer {
void stop();
void speakTTS( String text );
}