Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat:Improve the usage of the Recognizer in the Demo to reduce memory… #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions app/src/main/java/org/vosk/demo/VoskActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class VoskActivity extends Activity implements
private SpeechService speechService;
private SpeechStreamService speechStreamService;
private TextView resultView;
private Recognizer rec;

@Override
public void onCreate(Bundle state) {
Expand Down Expand Up @@ -115,6 +116,8 @@ public void onDestroy() {
speechService.shutdown();
}

closeRec();

if (speechStreamService != null) {
speechStreamService.stop();
}
Expand Down Expand Up @@ -204,10 +207,11 @@ private void recognizeFile() {
setUiState(STATE_DONE);
speechStreamService.stop();
speechStreamService = null;
closeRec();
} else {
setUiState(STATE_FILE);
try {
Recognizer rec = new Recognizer(model, 16000.f, "[\"one zero zero zero one\", " +
rec = new Recognizer(model, 16000.f, "[\"one zero zero zero one\", " +
"\"oh zero one two three four five six seven eight nine\", \"[unk]\"]");

InputStream ais = getAssets().open(
Expand All @@ -227,10 +231,11 @@ private void recognizeMicrophone() {
setUiState(STATE_DONE);
speechService.stop();
speechService = null;
closeRec();
} else {
setUiState(STATE_MIC);
try {
Recognizer rec = new Recognizer(model, 16000.0f);
rec = new Recognizer(model, 16000.0f);
speechService = new SpeechService(rec, 16000.0f);
speechService.startListening(this);
} catch (IOException e) {
Expand All @@ -239,6 +244,15 @@ private void recognizeMicrophone() {
}
}

/**
* Release the resources of the Recognizer object
*/
private void closeRec(){
if (rec!=null) {
rec.close();
rec = null;
}
}

private void pause(boolean checked) {
if (speechService != null) {
Expand Down