Created
April 12, 2016 03:44
-
-
Save kiichi/711df72ca51ba57e84e7978542317770 to your computer and use it in GitHub Desktop.
Speech Recognizer Android Example. I removed unrelated part of codes so it would not run if you just copy and paste it.
This file contains hidden or 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
// Ommitted ... | |
public class MainActivity extends AppCompatActivity | |
implements NavigationView.OnNavigationItemSelectedListener { | |
SpeechRecognizer mSpeech = null; | |
Intent mIntent; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Some UI Init code goes here ... | |
////////////////////////////////////////////////////////////////////////////////////// | |
// Note: | |
// 1. Under <manifest> | |
// <uses-permission android:name="android.permission.RECORD_AUDIO" /> | |
// 2. Then go to settings > Apps > Select the app > Permission > Turn on the Audio | |
////////////////////////////////////////////////////////////////////////////////////// | |
mSpeech = SpeechRecognizer.createSpeechRecognizer(this.getApplicationContext()); | |
mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); | |
mIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); | |
final Button btnToggle = (Button)findViewById(R.id.btnToggle); | |
btnToggle.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (btnToggle.getText() == "Start"){ | |
btnToggle.setText("Stop"); | |
mSpeech.startListening(mIntent); | |
} | |
else { | |
btnToggle.setText("Start"); | |
mSpeech.stopListening(); | |
} | |
} | |
}); | |
final TextView tvResult = (TextView)findViewById(R.id.tvResult); | |
tvResult.setText("Click Start"); | |
mSpeech.setRecognitionListener(new RecognitionListener() { | |
@Override | |
public void onReadyForSpeech(Bundle bundle) { | |
Log.v("myapp","onReadyForSpeech"); | |
tvResult.setText("Ready"); | |
} | |
@Override | |
public void onBeginningOfSpeech() { | |
Log.v("myapp","onBeginningOfSpeech"); | |
} | |
@Override | |
public void onRmsChanged(float v) { | |
Log.v("myapp","onRmsChanged:" + v); | |
if ((new Random()).nextInt(2) == 1) { | |
tvResult.setBackgroundColor(Color.LTGRAY); | |
} | |
else { | |
tvResult.setBackgroundColor(Color.GRAY); | |
} | |
} | |
@Override | |
public void onBufferReceived(byte[] bytes) { | |
Log.v("myapp","onBufferReceived: " + bytes.length); | |
} | |
@Override | |
public void onEndOfSpeech() { | |
Log.v("myapp","onEndOfSpeech"); | |
tvResult.setBackgroundColor(Color.WHITE); | |
} | |
@Override | |
public void onError(int i) { | |
//http://developer.android.com/reference/android/speech/SpeechRecognizer.html#ERROR_INSUFFICIENT_PERMISSIONS | |
Log.v("myapp","error: " + i); | |
tvResult.setBackgroundColor(Color.WHITE); | |
} | |
@Override | |
public void onResults(Bundle bundle) { | |
ArrayList<String> words = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); | |
float[] scores = bundle.getFloatArray(android.speech.SpeechRecognizer.CONFIDENCE_SCORES); | |
Log.v("myapp",words.get(0)); | |
//Log.v("myapp",scores[0])); | |
tvResult.setText(words.get(0)); | |
tvResult.setBackgroundColor(Color.WHITE); | |
} | |
@Override | |
public void onPartialResults(Bundle bundle) { | |
Log.v("myapp","onPartialResults"); | |
ArrayList<String> words = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); | |
tvResult.setText(words.get(0)); | |
tvResult.setBackgroundColor(Color.WHITE); | |
} | |
@Override | |
public void onEvent(int i, Bundle bundle) { | |
Log.v("myapp","onEvent:" + i); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment