2016-05-14 1 views
-1

s'il vous plaît me donner une suggestion à propos de cette condition. est-il possible dans android que l'appareil attend un son spécial, et après qu'il le reconnaît, certaines actions se produisent. s'il vous plaît parlez-moi de vos idées.Merci pour votre aide! .faire des actions après la reconnaissance du son dans android

MISE À JOUR i essayer poche et sphynx faire beaucoup de recherches sur « la définition nouveau mot-clé », mais je ne peux pas le faire j'utiliser ce 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()); 
} 

et ce pour digits.gram

my phone /1e-1/ 

cela se produit comme la course:

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: [email protected] 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)  

Que puis-je faire pour résoudre?

Répondre

1

Il est appelé «Reconnaissance vocale continue». Si vous essayez d'utiliser l'API SpeechRecognizer intégrée à Android avec une boucle infinie pour attendre certains mots ou certaines commandes, elle se déchargera sur beaucoup de batterie.

Vous pouvez regarder tutoriel CMUSphinx, il est beaucoup plus rapide et le dispositif convivial pour autant que je sais:

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

+0

merci beaucoup pour la meilleure suggestion, j'ai quelques questions que la vérification de ce tutoriel, pourquoi l'autorisation est vérifié à nouveau en classe? sauf si l'utilisateur les accepte au début de l'installation de l'application. 'onRequestPermissionsResult' et' 'onPartialResult' et' onResult' sont-ils nécessaires? –

+0

C'est le nouveau système d'autorisation Android 6.0. Vous devez demander à l'utilisateur dans l'interface utilisateur des autorisations importantes. onPartialResult n'est pas nécessaire mais onResult est le résultat d'un discours reconnu converti en texte, c'est donc important. – Ozgur

+0

Je ne sais pas pourquoi, après que mon application reconnaît ANDROID, il répète la méthode 'onPartialResult'? –