Click here to load reader
Upload
bruno-delb
View
82
Download
0
Embed Size (px)
Citation preview
Développer sur AndroidAndroid Lab Test
www.AndroidLabTest.com
Face
bo
ok
Par Bruno Delb
www.youtube.com/androidlabtest
www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com
www.facebook.com/Androidlabtest
You
tub
e
Site
off
icie
l
Leçon : La reconnaissance vocale
La reconnaissance vocale
• Dans cette leçon, vous allez apprendre à utiliser la
reconnaissance vocale.
• Pour cela, vous allez utiliser l’Intent
ACTION_RECOGNIZE_SPEECH.
La reconnaissance vocale
• Pour utiliser le système de reconnaissance vocale, utilisez l’intent ACTION_RECOGNIZE_SPEECH.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Talk now !");try {startActivityForResult(intent, 123);
} catch (ActivityNotFoundException e) {// Logiciel de reconnaissance vocale non installé
}
• Une fois que le moteur a analysé le son, il renvoie le résultat via l’intent.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode == 123) && resultCode == Activity.RESULT_OK) {
// Analyse du résultat}
}}
La reconnaissance vocale
• Si le logiciel de reconnaissance vocale n’a pas pu être trouvé, il peut être installé automatiquement :
Intent intentInstall = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.voicesearch")
);
• Dans la méthode onActivityResult, le résultat de l’analyse se trouve dans l’argument data sous forme d’ArrayList de String :
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);String text = "";for (String result : results) {text += result + "\n";
}et_output.setText (text);
Layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/et_output"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Enter a value" />
</LinearLayout>
Fichier Main.javapublic class Main extends Activity {
private static final int REQUEST_CODE = 1;
EditText et_output;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
et_output = (EditText)findViewById (R.id.et_output);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Talk now !");
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No recognition software installed. Download it.", Toast.LENGTH_SHORT).show();
Intent intentInstall = new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.google.android.voicesearch")
);
}
}
Fichier Main.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String text = "";
for (String result : results) {
text += result + "\n";
}
et_output.setText (text);
} else {
Toast.makeText(this, "Operation failed", Toast.LENGTH_SHORT).show();
}
}
}
}
Testez sur votre mobile
Media_VoiceRecognition
Retrouvez-moi sur ma chaîne AndroidLabTest …
Sur ma chaîne Youtubehttps://www.youtube.com/user/mobiledevlabtest
Qui suis-je ?
Bruno Delb,
auteur du 1er livre francophone de développement d’application Java sur mobile (2002),
développeur d’applications mobiles & sociales,
parlez-moi de vos projets.
Et bien sûr sur mon site Web :http://blog.brunodelb.com