-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFucUtil.java
More file actions
148 lines (137 loc) · 4.04 KB
/
Copy pathFucUtil.java
File metadata and controls
148 lines (137 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import android.content.Context;
/**
* 功能性函数扩展类
*/
/**
* 这个函数是通过官方文档直接获取的 没有进行使用 但是作为程序一部分可以继续扩展,因此先暂时存放在此处
*/
public class FucUtil {
/**
* 读取asset目录下文件。
* @return content
*/
public static String readFile(Context mContext,String file,String code)
{
int len = 0;
byte []buf = null;
String result = "";
try {
InputStream in = mContext.getAssets().open(file);
len = in.available();
buf = new byte[len];
in.read(buf, 0, len);
result = new String(buf,code);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 将字节缓冲区按照固定大小进行分割成数组
* @param buffer 缓冲区
* @param length 缓冲区大小
* @param spsize 切割块大小
* @return
*/
public ArrayList<byte[]> splitBuffer(byte[] buffer,int length,int spsize)
{
ArrayList<byte[]> array = new ArrayList<>();
if(spsize <= 0 || length <= 0 || buffer == null || buffer.length < length)
return array;
int size = 0;
while(size < length)
{
int left = length - size;
if(spsize < left)
{
byte[] sdata = new byte[spsize];
System.arraycopy(buffer,size,sdata,0,spsize);
array.add(sdata);
size += spsize;
}else
{
byte[] sdata = new byte[left];
System.arraycopy(buffer,size,sdata,0,left);
array.add(sdata);
size += left;
}
}
return array;
}
/**
* 获取语记是否包含离线听写资源,如未包含跳转至资源下载页面
*1.PLUS_LOCAL_ALL: 本地所有资源
2.PLUS_LOCAL_ASR: 本地识别资源
3.PLUS_LOCAL_TTS: 本地合成资源
*/
public static String checkLocalResource(){
String resource = SpeechUtility.getUtility().getParameter(SpeechConstant.PLUS_LOCAL_ASR);
try {
JSONObject result = new JSONObject(resource);
int ret = result.getInt(SpeechUtility.TAG_RESOURCE_RET);
switch (ret) {
case ErrorCode.SUCCESS:
JSONArray asrArray = result.getJSONObject("result").optJSONArray("asr");
if (asrArray != null) {
int i = 0;
// 查询否包含离线听写资源
for (; i < asrArray.length(); i++) {
if("iat".equals(asrArray.getJSONObject(i).get(SpeechConstant.DOMAIN))){
//asrArray中包含语言、方言字段,后续会增加支持方言的本地听写。
//如:"accent": "mandarin","language": "zh_cn"
break;
}
}
if (i >= asrArray.length()) {
SpeechUtility.getUtility().openEngineSettings(SpeechConstant.ENG_ASR);
return "没有听写资源,跳转至资源下载页面";
}
}else {
SpeechUtility.getUtility().openEngineSettings(SpeechConstant.ENG_ASR);
return "没有听写资源,跳转至资源下载页面";
}
break;
case ErrorCode.ERROR_VERSION_LOWER:
return "语记版本过低,请更新后使用本地功能";
case ErrorCode.ERROR_INVALID_RESULT:
SpeechUtility.getUtility().openEngineSettings(SpeechConstant.ENG_ASR);
return "获取结果出错,跳转至资源下载页面";
case ErrorCode.ERROR_SYSTEM_PREINSTALL:
//语记为厂商预置版本。
default:
break;
}
} catch (Exception e) {
SpeechUtility.getUtility().openEngineSettings(SpeechConstant.ENG_ASR);
return "获取结果出错,跳转至资源下载页面";
}
return "";
}
/**
* 读取asset目录下音频文件。
*
* @return 二进制文件数据
*/
public static byte[] readAudioFile(Context context, String filename) {
try {
InputStream ins = context.getAssets().open(filename);
byte[] data = new byte[ins.available()];
ins.read(data);
ins.close();
return data;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}