android音频合成代码,百度语音Android合成问题!
百度语音合成问题我在百度申请了账号, 也创建了应用,包名, appid,apikey,secrtkey都一样, 授权也通过了,但是就是播放不出来语音, 权限也加了!请问是什么问题. 求大神帮忙. 一下是我的完整代码!在每次播放前, 都将两个文件释放到内存卡上面, 然后设置给Baidu的Api最后代码都走到了 "授权成功" 那里了,但是就是不播放. 我也打了正式包. 也不行!是什么问题?...
百度语音合成问题
我在百度申请了账号, 也创建了应用,包名, appid,apikey,secrtkey都一样, 授权也通过了,但是就是播放不出来语音, 权限也加了!请问是什么问题. 求大神帮忙. 一下是我的完整代码!
在每次播放前, 都将两个文件释放到内存卡上面, 然后设置给Baidu的Api 最后代码都走到了 "授权成功" 那里了,但是就是不播放. 我也打了正式包. 也不行! 是什么问题? 授权的问题吗? 但是在官网的问答社区里面说最新的不需要设置授权文件啊! 求大神帮助!!!
public class MainActivity extends AppCompatActivity implements SpeechSynthesizerListener {
private static final String FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "bd_etts_speech_female.dat";
private static final String TEXT_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "bd_etts_text.dat";
private SpeechSynthesizer mSpeechSynthesizer;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_start = (Button) findViewById(R.id.bt_start);
bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(FILE_PATH);
File file1 = new File(TEXT_FILE_PATH);
Log.i(TAG, FILE_PATH);
if (file.exists()) {
Toast.makeText(MainActivity.this, "bd_etts_speech_female.dat", Toast.LENGTH_SHORT).show();
}
Log.i(TAG, TEXT_FILE_PATH);
if (file1.exists()) {
Toast.makeText(MainActivity.this, "bd_etts_text.dat", Toast.LENGTH_SHORT).show();
}
startTTS();
}
});
}
private void startTTS() {
// 获取语音合成对象实例
copyFileToSD("bd_etts_speech_female.dat");
copyFileToSD("bd_etts_text.dat");
mSpeechSynthesizer = SpeechSynthesizer.getInstance();
// 设置context
mSpeechSynthesizer.setContext(this);
// 设置语音合成状态监听器
mSpeechSynthesizer.setSpeechSynthesizerListener(this);
// 设置在线语音合成授权,需要填入从百度语音官网申请的api_key和secret_key
mSpeechSynthesizer.setApiKey("oDtDRyEtzIhuLyYDkVxXAKVf", "fb6c29c5a3e542c80d5d1a09bdeb0563");
// 设置离线语音合成授权,需要填入从百度语音官网申请的app_id
mSpeechSynthesizer.setAppId("9250086");
// 设置语音合成文本模型文件
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, TEXT_FILE_PATH);
// 设置语音合成声音模型文件
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, FILE_PATH);
// 获取语音合成授权信息
AuthInfo authInfo = mSpeechSynthesizer.auth(TtsMode.MIX);
// 判断授权信息是否正确,如果正确则初始化语音合成器并开始语音合成,如果失败则做错误处理
if (authInfo.isSuccess()) {
Toast.makeText(this, "授权成功", Toast.LENGTH_LONG).show();
mSpeechSynthesizer.initTts(TtsMode.MIX);
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "9");
mSpeechSynthesizer.speak("Hello,合成成功了!");
} else {
// 授权失败
Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onSynthesizeStart(String s) {
Log.i(TAG, "onSynthesizeStart");
}
@Override
public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {
Log.i(TAG, "onSynthesizeDataArrived: ");
}
@Override
public void onSynthesizeFinish(String s) {
Log.i(TAG, "onSynthesizeFinish: ");
}
@Override
public void onSpeechStart(String s) {
Log.i(TAG, "onSpeechStart: ");
}
@Override
public void onSpeechProgressChanged(String s, int i) {
Log.i(TAG, "onSpeechProgressChanged: ");
}
@Override
public void onSpeechFinish(String s) {
Log.i(TAG, "onSpeechFinish: ");
}
@Override
public void onError(String s, SpeechError speechError) {
Log.i(TAG, "onError: ");
}
private void copyFileToSD(String fileName) {
String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(absolutePath, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "拷贝文件失败1", Toast.LENGTH_LONG).show();
}
try {
InputStream open = getAssets().open(fileName);
OutputStream outputStream = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = open.read(b)) != -1) {
outputStream.write(b, 0, len);
}
open.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "拷贝文件失败2", Toast.LENGTH_LONG).show();
}
}
}
}
更多推荐
所有评论(0)