Last active
July 18, 2020 23:30
-
-
Save ksasao/e336f88e1d029aa573e0 to your computer and use it in GitHub Desktop.
Vuzix M100 v1.0.8 Voice Recognition sample. License: WTFPL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <uses-permission android:name="android.permission.RECORD_AUDIO"/> | |
package jp.mpga.voicerecognitionsample; | |
import java.util.ArrayList; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.speech.RecognitionListener; | |
import android.speech.RecognizerIntent; | |
import android.speech.SpeechRecognizer; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.Toast; | |
public class MainActivity extends Activity implements | |
OnClickListener, RecognitionListener | |
{ | |
SpeechRecognizer mSpeechRecognizer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// add listener | |
((Button) findViewById(R.id.button1)).setOnClickListener(this); | |
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); | |
mSpeechRecognizer.setRecognitionListener(this); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onReadyForSpeech(Bundle params) { | |
Toast.makeText(this, "Voice Recognition Ready.", Toast.LENGTH_SHORT).show(); | |
//iv.setImageResource(R.drawable.ready); | |
} | |
@Override | |
public void onBeginningOfSpeech() { | |
Toast.makeText(this, "Please speak command.", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onRmsChanged(float rmsdB) { | |
Log.v(AUDIO_SERVICE,"recieve : " + rmsdB + "dB"); | |
} | |
@Override | |
public void onBufferReceived(byte[] buffer) { | |
Log.v(AUDIO_SERVICE,"recieved"); | |
} | |
@Override | |
public void onEndOfSpeech() { | |
Log.v(AUDIO_SERVICE,"finished."); | |
} | |
@Override | |
public void onError(int error) { | |
Log.v(AUDIO_SERVICE,"Error: " + error); | |
} | |
@Override | |
public void onResults(Bundle results) { | |
// Vuzix M100 v1.0.8 only can support 'onPartialResuts' | |
Log.v(AUDIO_SERVICE,"onResults called"); | |
} | |
@Override | |
public void onPartialResults(Bundle partialResults) { | |
// Vuzix M100 v1.0.8 only can support 'onPartialResuts' | |
// Supported keywords: | |
// move left/right/up/down | |
// go left/right/up/down | |
// left/right/up/down | |
// next, previous, forward, back | |
// select, cancel | |
// complete, stop, exit, go home | |
// menu, volume up/down | |
// mute, call, dial, hang up, answer | |
// ignore, end, redial, call back | |
// contacts, favorites, pair, unpair | |
// sleep, shut down | |
// set clock/time | |
// cut, copy, paste, delete | |
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | |
String[] keywords = new String[]{"back", "next", "0", "1", "2", "3", "end"}; | |
ArrayList<String> recData = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); | |
String getData = new String(); | |
for (String s : recData) { | |
getData += s + ","; | |
} | |
// Show filtered keyword | |
String result = ""; | |
for (String s: recData){ | |
for (String t: keywords){ | |
if(s.equals(t)){ | |
result = t; | |
break; | |
} | |
} | |
if(!result.isEmpty()){ | |
Toast.makeText(this, result, Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
} | |
// Raw Result | |
Log.v(AUDIO_SERVICE,"Result: " + getData); | |
} | |
@Override | |
public void onEvent(int eventType, Bundle params) { | |
Log.v(AUDIO_SERVICE,"onEvent called"); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.button1: | |
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); | |
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, | |
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); | |
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, | |
getPackageName()); | |
mSpeechRecognizer.startListening(intent); | |
break; | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment