在android中进行声音识别后执行操作

时间:2016-05-14 10:29:34

标签: android voice-recognition cmusphinx pocketsphinx

请给我关于这个条件的建议。是否有可能在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) 

我该怎样做才能解决?

1 个答案:

答案 0 :(得分:1)

它被称为“连续语音识别”。如果您尝试使用Android内置的SpeechRecognizer api和无限循环来等待某些单词或命令,它将消耗大量电量。

你可以看看CMUSphinx教程,据我所知,它的速度和设备友好得多:

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