This commit is contained in:
wangcongtao
2020-04-14 18:24:28 +08:00
parent 338d0fa786
commit d243ccebf4
5 changed files with 36 additions and 8 deletions

View File

@@ -2,10 +2,12 @@ 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 com.mogo.utils.logger.Logger;
import java.util.HashMap;
import java.util.Locale;
/**
@@ -16,11 +18,12 @@ import java.util.Locale;
*/
public class AndroidTTSPlayer extends UtteranceProgressListener implements TTSPlayer, TextToSpeech.OnInitListener {
private static final String TAG = "AndroidTTSPlayer";
private TextToSpeech mTtsEngine;
private Context mContext;
private boolean mIsSuccess = false;
private Bundle mParams;
private HashMap< String, String > mParams;
/**
* {@link TextToSpeech.Engine#KEY_PARAM_STREAM},
@@ -32,9 +35,10 @@ public class AndroidTTSPlayer extends UtteranceProgressListener implements TTSPl
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 );
mParams = new HashMap<>();
mParams.put( TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_ALARM + "" );
mParams.put( TextToSpeech.Engine.KEY_PARAM_VOLUME, "1" );
mParams.put( TextToSpeech.Engine.KEY_PARAM_PAN, "0" );
}
@Override
@@ -46,7 +50,12 @@ public class AndroidTTSPlayer extends UtteranceProgressListener implements TTSPl
@Override
public void speakTTS( String text ) {
// mTtsEngine.speak()
if ( !mIsSuccess ) {
Logger.d( TAG, "do not support tts play." );
return;
}
mParams.put( TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text );
mTtsEngine.speak( text, TextToSpeech.QUEUE_FLUSH, mParams );
}
@Override

View File

@@ -0,0 +1,16 @@
package com.mogo.utils.tts;
import android.content.Context;
/**
* @author congtaowang
* @since 2020-04-14
* <p>
* 描述
*/
public class TTSPlayerFactory {
public static TTSPlayer getPlayer( Context context ) {
return new AndroidTTSPlayer( context );
}
}