本文介绍了在Android中识别声音后进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我有关这种情况的建议.在Android中是否有可能设备等待特殊声音,并且在识别到声音后便会发生一些动作.请告诉我您的想法.谢谢您的帮助!

please give me suggestion about this condition. is it possible in android that the device waits for special sound, and after it recognizes it, some actions happens.please tell me about your ideas.Thanks for your help!

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

UPDATEi try pocket sphinx and do lots of searches about 'defining new keyword", but i can't do it.I use this code:

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内置的SpeechRecognizer api等待某些单词或命令,则会消耗大量电量.

It is called 'Continuous Speech Recognition'. If you try to use Android's built-in SpeechRecognizer api with infinite loop to wait for certain words or commands, it will drain to much battery.

您可以查看CMUSphinx教程,据我所知它更快且对设备友好:

You can look at CMUSphinx tutorial, it's much faster and device-friendly as far as I know:

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

这篇关于在Android中识别声音后进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:53
查看更多