Skip to content

Commit 0629eae

Browse files
author
olami-developers
committed
fix audio record create problem
1 parent 55e6e73 commit 0629eae

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

examples/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 17
99
targetSdkVersion 25
1010
versionCode 1
11-
versionName "20171121"
11+
versionName "20171122"
1212

1313
archivesBaseName = "olami-android-hotword-examples"
1414

examples/src/main/java/ai/olami/android/example/HotwordDetectionActivity.java

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package ai.olami.android.example;
2020

2121
import android.Manifest;
22+
import android.content.Intent;
2223
import android.content.pm.PackageManager;
2324
import android.media.AudioFormat;
2425
import android.media.AudioRecord;
@@ -58,6 +59,11 @@ public class HotwordDetectionActivity extends AppCompatActivity {
5859
@Override
5960
protected void onCreate(Bundle savedInstanceState) {
6061
super.onCreate(savedInstanceState);
62+
}
63+
64+
protected void onResume() {
65+
super.onResume();
66+
6167
setContentView(R.layout.activity_main);
6268

6369
mDisplayText = (TextView) findViewById(R.id.displayText);
@@ -73,19 +79,14 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
7379
}
7480
}
7581
});
76-
}
7782

78-
protected void onResume() {
79-
super.onResume();
83+
hotwordDetectSwitchEnableChangeHandler(false);
8084

8185
Log.i(TAG, "checkPermission = "+ checkDeviceResourcePermissions());
8286
// Check hardware resource permissions
8387
if (checkDeviceResourcePermissions()) {
84-
createAudioRecord();
85-
8688
try {
8789
mDetectedCount = 0;
88-
startRecording();
8990
initializeHotwordDetection();
9091
startHotwordDetection();
9192
} catch (Exception ex) {
@@ -110,11 +111,12 @@ protected void onPause() {
110111
* Initial hotword detection
111112
*/
112113
private void initializeHotwordDetection() {
113-
createAudioRecord();
114-
startRecording();
115114
try {
116115
if (mHotwordDetect == null) {
117116

117+
createAudioRecord();
118+
startRecording();
119+
118120
// * Create HotwordDetect instance by the specified AudioRecord object.
119121
// ------------------------------------------------------------------------------
120122
// You should implement the IHotwordDetectListener to get all callbacks
@@ -153,20 +155,16 @@ private void startHotwordDetection() {
153155
// (1) Create AudioRecord object
154156
// (2) Start recording
155157
// (3) Re-assign to the HotwordDetect object.
158+
156159
createAudioRecord();
157160
startRecording();
158161
mHotwordDetect.setAudioRecord(mAudioRecord);
159162

160163
// * Now you can start hotword detection
161164
mHotwordDetect.startDetection();
162-
163-
hotwordDetectSwitchCheckChangeHandler(true);
164165
} catch (Exception ex) {
165166
ex.printStackTrace();
166167
}
167-
} else {
168-
initializeHotwordDetection();
169-
startHotwordDetection();
170168
}
171169
}
172170

@@ -184,7 +182,6 @@ private void stopHotwordDetection() {
184182
// the AudioRecord resource will be also released after this method called.
185183
mHotwordDetect.stopDetection();
186184

187-
hotwordDetectSwitchCheckChangeHandler(false);
188185
displayTextChangeHandler(getString(R.string.hotwordDetectIsClose));
189186

190187
} catch (Exception ex) {
@@ -205,6 +202,7 @@ private class HotwordDetectListener implements IHotwordDetectListener {
205202
public void onInitializing() {
206203
String str = getString(R.string.hotwordDetectOnInitializing);
207204

205+
hotwordDetectSwitchEnableChangeHandler(false);
208206
displayTextChangeHandler(str);
209207
Log.i(TAG, str);
210208
}
@@ -364,20 +362,44 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
364362
* Initial AudioRecord
365363
*/
366364
private void createAudioRecord() {
367-
int minBufferSize = AudioRecord.getMinBufferSize(
368-
44100,
369-
AudioFormat.CHANNEL_IN_MONO,
370-
AudioFormat.ENCODING_PCM_16BIT);
371-
372-
stopAndReleaseAudioRecord();
365+
if (mAudioRecord != null) {
366+
if (mAudioRecord.getState() == AudioRecord.STATE_UNINITIALIZED) {
367+
stopAndReleaseAudioRecord();
368+
}
369+
}
373370

374371
if (mAudioRecord == null) {
372+
int minBufferSize = AudioRecord.getMinBufferSize(
373+
44100,
374+
AudioFormat.CHANNEL_IN_MONO,
375+
AudioFormat.ENCODING_PCM_16BIT);
376+
375377
mAudioRecord = new AudioRecord(
376378
MediaRecorder.AudioSource.MIC,
377379
44100,
378380
AudioFormat.CHANNEL_IN_MONO,
379381
AudioFormat.ENCODING_PCM_16BIT,
380382
minBufferSize * 4);
383+
384+
// Waiting for AudioRecord initialized
385+
int retry = 0;
386+
while ((mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) && (retry < 4)) {
387+
try {
388+
Thread.sleep(500);
389+
} catch (InterruptedException ex) {
390+
ex.printStackTrace();
391+
}
392+
retry++;
393+
}
394+
395+
// Check AudioRecord is initialized or not
396+
if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
397+
if (mAudioRecord.getState() == AudioRecord.STATE_UNINITIALIZED) {
398+
throw new UnsupportedOperationException("Init AudioRecord failed.");
399+
} else {
400+
throw new UnknownError("Failed to initialize AudioRecord.");
401+
}
402+
}
381403
}
382404
}
383405

@@ -391,6 +413,18 @@ private void startRecording() {
391413

392414
if (mAudioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
393415
mAudioRecord.startRecording();
416+
417+
// Waiting for AudioRecord Recording
418+
int retry = 0;
419+
while ((mAudioRecord.getRecordingState()
420+
!= AudioRecord.RECORDSTATE_RECORDING) && (retry < 4)) {
421+
try {
422+
Thread.sleep(500);
423+
} catch (InterruptedException ex) {
424+
ex.printStackTrace();
425+
}
426+
retry++;
427+
}
394428
}
395429
}
396430

examples/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:id="@+id/displayText"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
13-
android:text="Hello World!"
13+
android:text="@string/hotwordDetectOnInitializing"
1414
app:layout_constraintBottom_toBottomOf="parent"
1515
app:layout_constraintLeft_toLeftOf="parent"
1616
app:layout_constraintRight_toRightOf="parent"

0 commit comments

Comments
 (0)