Created
September 18, 2017 10:08
-
-
Save mr-wind/a20c343febf3cd3f9318a46191350157 to your computer and use it in GitHub Desktop.
文字转语音工具类(需要有tts引擎)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.media.AudioAttributes; | |
import android.media.MediaPlayer; | |
import android.speech.tts.TextToSpeech; | |
import android.speech.tts.UtteranceProgressListener; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import com.gojiejiao.like.pos.Constants; | |
import java.math.BigDecimal; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.UUID; | |
/** | |
* Created by quhaofeng on 2017/6/19. | |
*/ | |
public class SpeakerUtil { | |
private static final String TAG = "TextToSpeech"; | |
private static SpeakerUtil mSpeakerUtil; | |
public MediaPlayer mMediaPlayer; | |
private Context context; | |
private TextToSpeech mTextToSpeech; | |
private ResultCallBack mResultCallBack; | |
private boolean initSuccess = false; | |
private boolean mSwitch = true;//功能开关 | |
public SpeakerUtil(Context context) { | |
this.context = context; | |
} | |
public static SpeakerUtil getInstance(Context context) { | |
mSpeakerUtil = new SpeakerUtil(context); | |
return mSpeakerUtil; | |
} | |
/** | |
* 播放本地音频 | |
* @param resId 本地资源ID | |
*/ | |
public void speak(int resId) { | |
mMediaPlayer = MediaPlayer.create(context, resId); | |
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
mp.reset(); | |
mp.release(); | |
} | |
}); | |
mMediaPlayer.start(); | |
} | |
/** | |
* 文字转语音 | |
* @param msg 转换的文本 | |
*/ | |
public void textToSpeak(final String msg) { | |
if (!mSwitch) { | |
return; | |
} | |
if (mTextToSpeech != null) { | |
//初始化不成功 | |
if (!initSuccess) { | |
mResultCallBack.onError(); | |
return; | |
} | |
HashMap<String, String> map = new HashMap<>(); | |
String utteranceId = UUID.randomUUID().toString(); | |
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId); | |
map.put(Constants.GOOGLE_TTS_PKG_NAME + ":engine", utteranceId); | |
int res = mTextToSpeech.speak(msg, TextToSpeech.QUEUE_ADD, map); | |
if (res != TextToSpeech.SUCCESS) { | |
Log.i(TAG, "onInit:res:" + res); | |
mResultCallBack.onError(); | |
mTextToSpeech = null; | |
} | |
return; | |
} | |
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() { | |
@Override | |
public void onInit(int status) { | |
if (status == TextToSpeech.SUCCESS) { | |
int result = mTextToSpeech.setLanguage(Locale.CHINA); | |
initSuccess = false; | |
if (result == TextToSpeech.LANG_MISSING_DATA) { | |
ToastUtil.showShortToast(context, "缺少语音包,请稍后重试"); | |
mResultCallBack.onError(); | |
} else if (result == TextToSpeech.LANG_NOT_SUPPORTED) { | |
ToastUtil.showShortToast(context, "不支持该语言"); | |
mResultCallBack.onError(); | |
} else if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE | |
&& result != TextToSpeech.LANG_AVAILABLE) { | |
Log.e(TAG, "onInit: TTS暂时不支持这种语言的朗读!"); | |
mResultCallBack.onError(); | |
} else { | |
initSuccess = true; | |
mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { | |
@Override | |
public void onStart(String utteranceId) { | |
Log.d("TextToSpeech", "onStart: "); | |
} | |
@Override | |
public void onDone(String utteranceId) { | |
Log.d("TextToSpeech", "onDone: "); | |
} | |
@Override | |
public void onError(String utteranceId) { | |
Log.d("TextToSpeech", "onErr: "); | |
mResultCallBack.onError(); | |
} | |
}); | |
HashMap<String, String> map = new HashMap<>(); | |
String utteranceId = UUID.randomUUID().toString(); | |
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId); | |
map.put(Constants.GOOGLE_TTS_PKG_NAME + ":engine", utteranceId); | |
int res = mTextToSpeech.speak(msg, TextToSpeech.QUEUE_ADD, map); | |
if (res != TextToSpeech.SUCCESS) { | |
Log.i(TAG, "onInit:res:" + res); | |
mResultCallBack.onError(); | |
} | |
} | |
} else { | |
Log.i(TAG, "onInit: 连接TTS引擎失败"); | |
mResultCallBack.onError(); | |
} | |
} | |
}, Constants.GOOGLE_TTS_PKG_NAME); | |
} | |
public void releaseSpeaker() { | |
if (mTextToSpeech != null) { | |
mTextToSpeech.stop(); | |
mTextToSpeech.shutdown(); | |
mTextToSpeech = null; | |
} | |
} | |
public void setResultCallBack(ResultCallBack resultCallBack) { | |
mResultCallBack = resultCallBack; | |
} | |
public interface ResultCallBack { | |
void onSuccess(); | |
void onError(); | |
} | |
/** | |
* 金额的文字转换 | |
* | |
* @param money 要转换的金额 | |
* @return 返回字符串 | |
*/ | |
public static String money2text(double money) { | |
StringBuilder sb = new StringBuilder(); | |
int tempMoney = MathExtendUtil.multiplyInt(money, 100); | |
if (tempMoney >= 100) { | |
sb.append(tempMoney / 100).append("元"); | |
} | |
if (tempMoney % 100 >= 10) { | |
sb.append((tempMoney % 100) / 10).append("角"); | |
} | |
if (tempMoney % 10 > 0) { | |
sb.append(tempMoney % 10).append("分"); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment