/**
* Copy Right (c)塘上科技,tangsci.cn
* Project :TangsonicLite Android版
* Version :2
* Modification history :2016.1.18
* Author :BaoWei
* brief 即将播放任务会打断优先级低或者相同优先级的播放任务
*/
package com.tangsci.android.tts;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.tangsci.tts.TtsEngine;
/*
* 注意这个文件一般不需要改动
*/
public class TtsPlayer
{
private static final byte TTSPLAY_STATE_STOP = 0;
private static final byte TTSPLAY_STATE_PLAY = 1;
private static final byte TTSPLAY_STATE_PAUSE = 2;
private static final byte TTSPLAY_STATE_EXIT = 3;
private static final byte TTSPLAY_STATE_READY = 4;
private byte[] m_playLock = new byte[0];
private byte[] m_monitorLock = new byte[0];
private byte m_state;
private TtsEngine m_ttsEngine;
private PlayThread m_playThread;
private int m_sampleRate = 16000;
private int m_sessionId = 0;
private boolean m_isInit = false;
private int m_currentLevel = -1;
private int m_nextLevel = -1;
private String m_nextPlayText = null;
Handler m_msgHandler = null;
public TtsPlayer()
{
int sampleRate = 16000;
if (sampleRate > 22050)
{
m_sampleRate = 22050;
}
else if (sampleRate < 8000)
{
m_sampleRate = 8000;
}
else
{
m_sampleRate = sampleRate;
}
}
public TtsPlayer(Handler msgHandler)
{
this();
m_msgHandler = msgHandler;
}
public boolean isIdle()
{
synchronized(m_monitorLock)
{
return getState() == TTSPLAY_STATE_READY;
}
}
public void setMsgHandler(Handler msgHandler)
{
synchronized(m_monitorLock)
{
m_msgHandler = msgHandler;
}
}
protected byte getState()
{
synchronized(m_monitorLock)
{
return m_state;
}
}
private void setIdle()
{
setStateFlag(TTSPLAY_STATE_READY);
}
protected void setStateFlag(byte state)
{
m_state = state;
}
private void setState(byte state)
{
synchronized(m_monitorLock)
{
if (state == TTSPLAY_STATE_PAUSE && m_state != TTSPLAY_STATE_PLAY)
{
return;
}
setStateFlag(state);
synchronized (m_playLock)
{
m_playLock.notifyAll();
}
}
}
public boolean initEngine(byte[] ttsResource)
{
synchronized(m_monitorLock)
{
if (!m_isInit)
{
m_ttsEngine = new TtsEngine();
if (TtsEngine.TSERR_SUCCESS == m_ttsEngine.init(ttsResource))
{
Integer intSessionId = Integer.valueOf(0);
if (TtsEngine.TSERR_SUCCESS == m_ttsEngine.newSession(intSessionId))
{
m_sessionId = intSessionId.intValue();
m_ttsEngine.setParam(m_sessionId, "Encoding", TtsEngine.ENCODING_UTF8);
m_ttsEngine.setParam(m_sessionId, "SampleRate", Integer.valueOf(m_sampleRate).toString());
m_isInit = true;
}
else
{
m_sessionId = 0;
return false;
}
}
else
{
m_sessionId = 0;
return false;
}
m_state = TTSPLAY_STATE_STOP;
m_playThread = new PlayThread();
m_playThread.start();
setIdle();
}
return true;
}
}
public int uninitEngine()
{
synchronized(m_monitorLock)
{
m_state = TTSPLAY_STATE_EXIT;
synchronized (m_playLock)
{
m_playLock.notifyAll();
}
if (null != m_playThread)
{
try
{
m_playThread.join(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
int rt;
rt = m_ttsEngine.delSession(m_sessionId);
rt = m_ttsEngine.uninit();
m_ttsEngine = null;
m_playThread = null;
m_isInit = false;
m_sessionId = 0;
return rt;
}
}
public boolean setParam(String strName, String strValue)
{
synchronized(m_monitorLock)
{
int rt;
if (null == m_ttsEngine)
{
return false;
}
rt = m_ttsEngine.setParam(m_sessionId, strName, strValue);
if (TtsEngine.TSERR_SUCCESS == rt)
{
return true;
}
else
{
return false;
}
}
}
public String getParam(String strName)
{
synchronized(m_monitorLock)
{
String rtValue;
if (null == m_ttsEngine)
{
return "";
}
rtValue = m_ttsEngine.getParam(m_sessionId, strName);
return rtValue;
}
}
public boolean setGlobalParam(String strName, String strValue)
{
synchronized(m_monitorLock)
{
int rt;
int sessionId = 0;
if (null == m_ttsEngine)
{
return false;
}
rt = m_ttsEngine.setParam(sessionId, strName, strValue);
if (TtsEngine.TSERR_SUCCESS == rt)
{
return true;
}
else
{
return false;
}
}
}
public String getGlobalParam(String strName)
{
synchronized(m_monitorLock)
{
String rtValue;
int sessionId = 0;
if (null == m_ttsEngine)
{
return "";
}
rtValue = m_ttsEngine.getParam(sessionId, strName);
return rtValue;
}
}
//level>=0才有效
public boolean playText(String inputText, int level)
{
synchronized(m_monitorLock)
{
return _doPlay(inputText, level);
}
}
public boolean playText(String inputText)
{
return playText(inputText, 0);
}
private boolean _doPlay(String inputText, int levelOrFlag)
{
if (inputText == null && levelOrFlag == -10000)//合成线程在调用
{
boolean needSendStopFinshMsg = (m_currentLevel >= 0);
if (m_nextLevel >= 0 && m_nextPlayText != null)
{
m_currentLevel = m_nextLevel;
m_nextLevel = -1;
String strTemp = m_nextPlayText;
m_nextPlayText = null;
_playText(strTemp);
}
else
{
m_currentLevel = -1;
}
if (m_msgHandler != null && needSendStopFinshMsg)
{
Message msg = new Message();
Bundle b = new Bundle();
b.putString("play_state", "idle");
msg.setData(b);
m_msgHandler.sendMessage(msg);///<告诉界面线程(主线程)播放已经结束或者已经终止完成了
}
return true;
}
else
{
int level = levelOrFlag;
if (-1 == m_currentLevel)
{
m_currentLevel = level;
_playText(inputText);
return true;
}
else if (level >= 0 && level >= m_currentLevel && level >= m_nextLevel)
{
m_nextLevel = level;
m_nextPlayText = inputText;
setState(TTSPLAY_STATE_STOP);
return true;
}
else
{
return false;
}
}
}
private boolean _playText(String inputText)
{
int rt;
if (null == m_ttsEngine || !isIdle())
{
if (!isIdle())
{
Log.w("TtsPlayer", "Not ready for next Play");
}
return false;
}
rt = m_ttsEngine.inputText(m_sessionId, inputText.getBytes(), false);
if (TtsEngine.TSERR_SUCCESS == rt)
{
setState(TTSPLAY_STATE_PLAY);
return true;
}
else
{
Log.w("TtsPlayer", "Input text error");
return false;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TtsDemo源码.zip (25个子文件)
TtsDemo
.project 851B
project.properties 563B
src
com
tangsci
tts
TtsEngine.java 7KB
android
tts
TtsPlayer.java 13KB
example
ttsdemo
TtsDemoActivity.java 11KB
AndroidManifest.xml 844B
.idea
misc.xml 409B
compiler.xml 842B
workspace.xml 59KB
codeStyleSettings.xml 324B
.name 7B
encodings.xml 166B
modules.xml 256B
scopes
scope_settings.xml 139B
copyright
profiles_settings.xml 111B
vcs.xml 166B
res
raw
ttsres 775KB
drawable-ldpi
drawable-hdpi
drawable-mdpi
layout
main.xml 5KB
drawable-xhdpi
values
strings.xml 711B
dimens.xml 225B
.settings
org.eclipse.jdt.core.prefs 177B
org.eclipse.core.resources.prefs 57B
assets
gen
libs
armeabi-v7a
libtstts_jni.so 142KB
.classpath 364B
bin
AndroidManifest.xml 844B
共 25 条
- 1
百世修行
- 粉丝: 159
- 资源: 88
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页