package com.exam.byuyin;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.chainofresponsibility.logger.LoggerProxy;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity implements SpeechSynthesizerListener {
// 语音合成客户端
private SpeechSynthesizer mSpeechSynthesizer;
private String mSampleDirPath;
private static final String SAMPLE_DIR_NAME = "VoiceTest";
private static final String SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female.dat";
private static final String SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male.dat";
private static final String TEXT_MODEL_NAME = "bd_etts_text.dat";
private static final String LICENSE_FILE_NAME = "temp_license";
private static final String ENGLISH_SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female_en.dat";
private static final String ENGLISH_SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male_en.dat";
private static final String ENGLISH_TEXT_MODEL_NAME = "bd_etts_text_en.dat";
private LoggerProxy loggerProxy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loggerProxy = new LoggerProxy();
initialEnv();
startTTS();
findViewById(R.id.Button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = mSpeechSynthesizer.speak("小时候我一次在家写作业,突然停电了,耶耶耶耶耶!终于不用写作业了,可以\n")
;
Log.e("Voice", result + "");
}
});
loggerProxy.printable(true);
}
private void initialEnv() {
if (mSampleDirPath == null) {
String sdcardPath = Environment.getExternalStorageDirectory().toString();
mSampleDirPath = sdcardPath + "/" + SAMPLE_DIR_NAME;
}
makeDir(mSampleDirPath);
copyFromAssetsToSdcard(false, SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME);
copyFromAssetsToSdcard(false, SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_MALE_MODEL_NAME);
copyFromAssetsToSdcard(false, TEXT_MODEL_NAME, mSampleDirPath + "/" + TEXT_MODEL_NAME);
copyFromAssetsToSdcard(false, LICENSE_FILE_NAME, mSampleDirPath + "/" + LICENSE_FILE_NAME);
copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/"
+ ENGLISH_SPEECH_FEMALE_MODEL_NAME);
copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/"
+ ENGLISH_SPEECH_MALE_MODEL_NAME);
copyFromAssetsToSdcard(false, "english/" + ENGLISH_TEXT_MODEL_NAME, mSampleDirPath + "/"
+ ENGLISH_TEXT_MODEL_NAME);
}
private void makeDir(String dirPath) {
File file = new File(dirPath);
if (!file.exists()) {
file.mkdirs();
}
}
/**
* 将sample工程需要的资源文件拷贝到SD卡中使用(授权文件为临时授权文件,请注册正式授权)
*
* @param isCover 是否覆盖已存在的目标文件
* @param source
* @param dest
*/
private void copyFromAssetsToSdcard(boolean isCover, String source, String dest) {
File file = new File(dest);
if (isCover || (!isCover && !file.exists())) {
InputStream is = null;
FileOutputStream fos = null;
try {
is = getResources().getAssets().open(source);
String path = dest;
fos = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int size = 0;
while ((size = is.read(buffer, 0, 1024)) >= 0) {
fos.write(buffer, 0, size);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 初始化语音合成客户端并启动
private void startTTS() {
// 获取语音合成对象实例
mSpeechSynthesizer = SpeechSynthesizer.getInstance();
// 设置context
mSpeechSynthesizer.setContext(this);
// 设置语音合成状态监听器
mSpeechSynthesizer.setSpeechSynthesizerListener(this);
// 设置在线语音合成授权,需要填入从百度语音官网申请的api_key和secret_key
mSpeechSynthesizer.setApiKey("WzuXRzb1xfLDwpdaeWKufmEE", "oibQpE76js2rftop664lu1bO9GvCMC2o");
// 设置离线语音合成授权,需要填入从百度语音官网申请的app_id
mSpeechSynthesizer.setAppId("9585097");
//设置网络模式
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_DEFAULT);
// 设置语音合成文本模型文件
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, mSampleDirPath + "/" + TEXT_MODEL_NAME);
// 设置语音合成声音模型文件
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME);
// 设置语音合成声音授权文件
// mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_LICENCE_FILE, mSampleDirPath + "/" + LICENSE_FILE_NAME);
// 获取语音合成授权信息
AuthInfo authInfo = mSpeechSynthesizer.auth(TtsMode.MIX);
// 判断授权信息是否正确,如果正确则初始化语音合成器并开始语音合成,如果失败则做错误处理
if (authInfo.isSuccess()) {
mSpeechSynthesizer.initTts(TtsMode.MIX);
mSpeechSynthesizer.speak("语音启动");
} else {
// 授权失败
Log.e("Voice", authInfo.getTtsError().getDetailMessage());
}
}
@Override
public void onSynthesizeStart(String s) {
// 监听到合成开始,在此添加相关操作
}
@Override
public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {
// 监听到有合成数据到达,在此添加相关操作
}
@Override
public void onSynthesizeFinish(String s) {
// 监听到合成结束,在此添加相关操作
Log.e("Voice", s);
}
@Override
public void onSpeechStart(String s) {
// 监听到合成并播放开始,在此添加相关操作
}
@Override
public void onSpeechProgressChanged(String s, int i) {
// 监听到播放进度有变化,在此添加相关操作
}
@Override
public void onSpeechFinish(String s) {
// 监听到播放结束,在此添加相关操作
}
@Override
public void onError(String s, SpeechError speechError) {
// 监听到出错,在此添加相关操作
}
}
百度语音合成
最新推荐文章于 2021-05-08 15:55:06 发布