我希望我的设备持续收听关键字。为此,我正在使用 PocketSphinx。我在 github 上找到了一个演示项目,它使 PocketSphinx 可以使用 Service 持续监听。
它第一次工作正常,但在那个假设之后。Hypstr() 一次又一次地返回相同的字符串。
我想清除 hypotheses.Hypstr() 中的字符串,以使这个应用程序对我正常工作。
这是我的服务类:
package com.hrupin.speechrecognitionservice;
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import edu.cmu.pocketsphinx.SpeechRecognizer;
import edu.cmu.pocketsphinx.SpeechRecognizerSetup;
/**
* Created by Igor Khrupin www.hrupin.com on 6/3/16.
*/
public class VoiceService extends Service implements
RecognitionListener {
private static final String LOG_TAG = VoiceService.class.getSimpleName();
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "hello";
private SpeechRecognizer recognizer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Check if user has given permission to record audio
int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
}
return super.onStartCommand(intent, flags, startId);
}
private void runRecognizerSetup() {
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(VoiceService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if (result != null) {
Log.i(LOG_TAG, "Failed to init recognizer ");
} else {
switchSearch(KWS_SEARCH);
}
}
}.execute();
}
@Override
public void onDestroy() {
super.onDestroy();
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
}
}
/**
* In partial result we get quick updates about current hypothesis. In
* keyword spotting mode we can react here, in other modes we need to wait
* for final result in onResult.
*/
@Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
if (text.contains(KEYPHRASE)) {
Toast.makeText(this, "onPartialResult text=" + text, Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
Log.i(LOG_TAG, "onPartialResult text=" +text);
}
/**
* This callback is called when we stop the recognizer.
*/
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.i(LOG_TAG, "onResult text=" +text);
}
}
@Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
}
/**
* We stop recognizer here to get a final result
*/
@Override
public void onEndOfSpeech() {
if (!recognizer.getSearchName().contains(KWS_SEARCH))
switchSearch(KWS_SEARCH);
Log.i(LOG_TAG, "onEndOfSpeech");
}
private void switchSearch(String searchName) {
Log.i(LOG_TAG, "switchSearch searchName = " + searchName);
recognizer.cancel();
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
recognizer.startListening(searchName, 10000);
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.setKeywordThreshold(1e-45f) // Threshold to tune for keyphrase to balance between false alarms and misses
.setBoolean("-allphone_ci", true) // Use context-independent phonetic search, context-dependent is too slow for mobile
.getRecognizer();
recognizer.addListener(this);
/** In your application you might not need to add all those searches.
* They are added here for demonstration. You can leave just one.
*/
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
}
@Override
public void onError(Exception error) {
Log.i(LOG_TAG, "onError " + error.getMessage());
}
@Override
public void onTimeout() {
switchSearch(KWS_SEARCH);
Log.i(LOG_TAG, "onTimeout");
}
}
和 Logcat 启动服务后,以防万一有人需要:
Speechrecognitionservice V/ViewRootImpl: 调整 android.view.ViewRootImpl@3b6eb36: frame=[222,1596][857,1728] contentInsets=[0,0][0,0] visibleInsets=[0,0][0,0] reportDraw=true 02-17 14:27:52.937 10121-10135/com.hrupin.speechrecognitionservice I/libGameXtend: LUCID_1 (1487321872937) GameXtend 当前在手动模式下运行 02-17 14:27:52.937 10121-10135/com.hrup .speechrecognitionservice I/libGameXtend: LUCID_1 (1487321872937) GameXtend 当前在 no_save 保存电源状态下运行。02-17 14:27:52.937 10121-10135/com.hrupin.speechrecognitionservice I/libGameXtend: LUCID_1 (1487321872937) com.hrupin.speechrecognitionservice 当前与 GameXtend 一起运行,PS 参数 = 1。不支持 ICE。PS参数来源为PowerXtend配置02-17 14:27:52.937 10121-10135/com.hrupin。
注意:我想重置hypothesis.getHypstr() 字符串,这样我的程序下次可以正常工作。
我认为这足以解释,但如果我遗漏了什么,请在评论中告诉我。
谢谢你。