在安卓中声音识别后执行操作



请给我关于这种情况的建议。 在Android中,设备是否有可能等待特殊声音,并在识别出它后发生一些操作。请告诉我你的想法。感谢您的帮助!

更新我尝试了袖珍狮身人面像,并做了很多关于"定义新关键字"的搜索,但我做不到。我使用此代码:

public class PracticeActivity  extends Activity implements RecognitionListener, edu.cmu.pocketsphinx.RecognitionListener {

 // private static final String KWS_SEARCH = "wakeup";
   // private static final String KEYPHRASE = "listen"; //adjust this keyphrase!
   //3-
   private static final String DIGITS_SEARCH = "digits";
private edu.cmu.pocketsphinx.SpeechRecognizer recognizer;
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.practice);
    ((TextView) findViewById(R.id.caption_text))
            .setText("Preparing the recognizer");
    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(PracticeActivity.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }
        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                ((TextView) findViewById(R.id.caption_text))
                        .setText("Failed to init recognizer " + result);
            } else {
                recognizer.startListening(DIGITS_SEARCH);
            }
        }
    }.execute();
}
@Override
public void onDestroy() {
    super.onDestroy();
    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.equals("my phone")) {
        ((TextView) findViewById(R.id.result_text))
                .setText(text);
        findViewById(R.id.result).setVisibility(View.VISIBLE);
       /* recognizer.cancel();
        recognizer.startListening(KWS_SEARCH);*/
    }else if (text.equals("where is my phone")){
        ((TextView) findViewById(R.id.caption_text))
                .setText(text);
        ((TextView) findViewById(R.id.result_text))
                .setText("i am here");
    }else
        recognizer.startListening(DIGITS_SEARCH);
}
@Override
public void onResult(Hypothesis hypothesis) {
}
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
@Override
public void onTimeout() {
}
private void setupRecognizer(File assetsDir) throws IOException {
    File modelsDir = new File(assetsDir, "models");
    // The recognizer can be configured to perform multiple searches
    // of different kind and switch between them
    recognizer = defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            .getRecognizer();
    recognizer.addListener(this);

    /*/2-
    File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);*/
    //3-
    File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
    recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
}
@Override
public void onError(Exception error) {
    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
}

这是为了digits.gram

my phone /1e-1/

这发生在运行时:

06-08 15:37:38.300 27871-27909/phone_finder.maxsoft.com.whereismyphone I/OpenGLRenderer: Initialized EGL, version 1.4
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0x7f73c27be0 ,&mEglDisplay = 1 , &mEglConfig = 1943265968 
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone D/OpenGLRenderer: Enabling debug mode 0
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
06-08 15:37:38.320 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(320): Allocating 137521 * 32 bytes (4297 KiB) for word entries
06-08 15:37:38.320 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(333): Reading main dictionary: /storage/emulated/0/Android/data/phone_finder.maxsoft.com.whereismyphone/files/sync/cmudict-en-us.dict
06-08 15:37:38.390 27871-27871/phone_finder.maxsoft.com.whereismyphone I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2596fa2 time:22196983
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(213): Allocated 1007 KiB for strings, 1662 KiB for phones
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(336): 133420 words read
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(358): Reading filler dictionary: /storage/emulated/0/Android/data/phone_finder.maxsoft.com.whereismyphone/files/sync/en-us-ptm/noisedict
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(213): Allocated 0 KiB for strings, 0 KiB for phones
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(361): 5 words read
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(396): Building PID tables for dictionary
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(406): Allocating 42^3 * 2 bytes (144 KiB) for word-initial triphones
06-08 15:37:38.590 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(132): Allocated 42672 bytes (41 KiB) for word-final triphones
06-08 15:37:38.590 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(196): Allocated 42672 bytes (41 KiB) for single-phone word triphones
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: kws_search.c(420): KWS(beam: -1080, plp: -23, default threshold 0, delay 10)
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone E/cmusphinx: ERROR: "kws_search.c", line 351: Failed to open keyword file '/storage/emulated/0/Android/data/phone_finder.maxsoft.com.whereismyphone/files/sync/models/grammar/digits.gram': No such file or directory
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone E/cmusphinx: ERROR: "kws_search.c", line 424: Failed to create kws search
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                         Process: phone_finder.maxsoft.com.whereismyphone, PID: 27871
                                                                                         java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                             at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                                             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                             at java.lang.Thread.run(Thread.java:818)
                                                                                          Caused by: java.lang.RuntimeException: Decoder_setKws returned -1
                                                                                             at edu.cmu.pocketsphinx.PocketSphinxJNI.Decoder_setKws(Native Method)
                                                                                             at edu.cmu.pocketsphinx.Decoder.setKws(Decoder.java:151)
                                                                                             at edu.cmu.pocketsphinx.SpeechRecognizer.addKeywordSearch(SpeechRecognizer.java:276)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity.setupRecognizer(PracticeActivity.java:169)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity.access$000(PracticeActivity.java:25)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity$1.doInBackground(PracticeActivity.java:47)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity$1.doInBackground(PracticeActivity.java:41)
                                                                                             at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                             at java.lang.Thread.run(Thread.java:818) 

我能做些什么来解决?

它被称为"连续语音识别"。如果您尝试使用具有无限循环的Android内置语音识别器API来等待某些单词或命令,它将耗尽大量电池电量。

你可以看看CMUSphinx教程,据我所知,它更快,设备友好:

http://cmusphinx.sourceforge.net/wiki/tutorialandroid

最新更新