注: 官网下载的SDK和操作手册不匹配,操作手册上的一些方法在新的SDK里并没有。如设置语音朗读速度,语调什么的方法在新版SDK中都无法找到.因此将在网上找到的资料整理了一下。
核心就是两个类,一个负责将asset中的文件拷贝到手机本地的文件夹中。另一个类就是初始化引擎并使用
中转,确定源路劲和目标路径
public class OfflineResource {
private AssetManager assets;
private String destPath;
private String backFilename;
private String modelFilename;
public OfflineResource(Context context) throws IOException {
this.assets = context.getAssets();
this.destPath = FileUtils.createTmpDir(context);
setOfflineVoiceType();
}
public String getModelFilename() {
return modelFilename;
}
public String getBackFilename() {
return backFilename;
}
public void setOfflineVoiceType() throws IOException {
String back = "backend_lzl";
String model = "frontend_model";
backFilename = copyAssetsFile(back);
modelFilename = copyAssetsFile(model);
}
private String copyAssetsFile(String sourceFilename) throws IOException {
String destFilename = destPath + "/" + sourceFilename;
FileUtils.copyFromAssets(assets, sourceFilename, destFilename, false);
Log.i(TAG, "Assets to sdcard successed:" + destFilename);
return destFilename;
}
}
2获取路径及开始进行文件复制
public final class FileUtils {
private static final String LOGTAG ="/ing/tts";
private static FileUtils fileUtils = new FileUtils();
private FileUtils() {
}
public static FileUtils getInstance() {
return fileUtils;
}
/**
* 获取SDCARD根路径
*
* @return
*/
private static StringBuffer getRootDir() throws Exception {
return new StringBuffer().append(Environment
.getExternalStorageDirectory());
}
/**
* 获取存在SDCARD上文件的绝对路径
*
* @param mContext
* @param folderName
* @param fileName
* @return
* @throws Exception
*/
public static StringBuffer getExternalFileAbsoluteDir(Context mContext,