| 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | package com.example.administrator.baiduvoicetest; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.AndroidRuntimeException; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.baidu.speech.EventListener; import com.baidu.speech.EventManager; import com.baidu.speech.EventManagerFactory; import com.baidu.tts.auth.AuthInfo; 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 org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private TextView txtResult; private EditText mInput; private EventManager mWpEventManager; private SpeechSynthesizer mSpeechSynthesizer; private String mSampleDirPath; private static final String SAMPLE_DIR_NAME = "baiduTTS" ; 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 static final String TAG = "MainActivity" ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtResult = (TextView) findViewById(R.id.txtResult); txtResult.setText( "请说唤醒词: 小度你好或者百度一下\n\n" + "离在线语法识别(首次使用需要联网授权)\n" + "语音识别开始后你可以说(可以根据语法自行定义离线说法):\n" + " 1. 打电话给张三(离线)\n" + " 2. 打电话给李四(离线)\n" + " 3. 打开计算器(离线)\n" + " 4. 明天天气怎么样(需要联网)\n" + " ..." + "\n" ); mInput= (EditText) findViewById(R.id.input); mInput.setVisibility(View.GONE); initialEnv(); initialTts(); } @Override protected void onResume() { super .onResume(); mWpEventManager = EventManagerFactory.create(MainActivity. this , "wp" ); mWpEventManager.registerListener( new EventListener() { @Override public void onEvent(String name, String params, byte [] data, int offset, int length) { Log.d(TAG, String.format( "event: name=%s, params=%s" , name, params)); try { JSONObject json = new JSONObject(params); if ( "wp.data" .equals(name)) { String word = json.getString( "word" ); txtResult.append( "唤醒成功, 唤醒词: " + word + "\r\n" ); mInput.setText( "唤醒成功,请说出指令" ); Toast.makeText(MainActivity. this ,mInput.getText(),Toast.LENGTH_LONG).show(); speak(); try { Thread.sleep( 3000 ); } catch (InterruptedException e) { e.printStackTrace(); } Intent intent = new Intent( "com.baidu.action.RECOGNIZE_SPEECH" ); intent.putExtra( "grammar" , "asset:///baidu_speech_grammardemo.bsg" ); // 设置离线的授权文件(离线模块需要授权), 该语法可以用自定义语义工具生成, 链接http://yuyin.baidu.com/asr#m5 startActivityForResult(intent, 1 ); } else if ( "wp.exit" .equals(name)) { txtResult.append( "唤醒已经停止: " + params + "\r\n" ); } } catch (JSONException e) { throw new AndroidRuntimeException(e); } } }); HashMap params = new HashMap(); params.put( "kws-file" , "assets:///WakeUpDemo.bin" ); // 设置唤醒资源, 唤醒资源请到 http://yuyin.baidu.com/wake#m4 来评估和导出 mWpEventManager.send( "wp.start" , new JSONObject(params).toString(), null , 0 , 0 ); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Bundle results = data.getExtras(); ArrayList results_recognition = results.getStringArrayList( "results_recognition" ); String str=results_recognition+ "" ; String res=str.substring(str.indexOf( "[" )+ 1 ,str.indexOf( "]" )); txtResult.append(res+ "\n" ); mInput.setText( "好的,马上执行" +res); speak(); Toast.makeText(MainActivity. this ,mInput.getText(),Toast.LENGTH_LONG).show(); } } private void initialTts() { this .mSpeechSynthesizer = SpeechSynthesizer.getInstance(); this .mSpeechSynthesizer.setContext( this ); this .mSpeechSynthesizer.setSpeechSynthesizerListener( new SpeechSynthesizerListener() { @Override public void onSynthesizeStart(String s) { } @Override public void onSynthesizeDataArrived(String s, byte [] bytes, int i) { } @Override public void onSynthesizeFinish(String 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) { } }); this .mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, mSampleDirPath + "/" + TEXT_MODEL_NAME); this .mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME); this .mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_LICENCE_FILE, mSampleDirPath + "/" + LICENSE_FILE_NAME); this .mSpeechSynthesizer.setAppId( "xxx" 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(); } } } } @Override protected void onStop() { super .onStop(); mWpEventManager.send( "wp.stop" , null , null , 0 , 0 ); } } |