Permalink
Browse files

Localize VoiceRecognizer API

  • Loading branch information...
1 parent b5b5297 commit 62e493bd522fefa903ebd9b7d2450272d1ce452a @timsu timsu committed Aug 13, 2012
@@ -5,58 +5,55 @@
import java.io.IOException;
import java.util.ArrayList;
+import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.media.MediaPlayer;
-import android.net.Uri;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
-import android.view.View;
-import android.view.View.OnClickListener;
-import android.widget.TextView;
-import android.widget.Toast;
+@TargetApi(8)
public class RecognizerApi implements RecognitionListener {
public static interface PlaybackExceptionHandler {
public void playbackFailed(String file);
}
-
+
private String aacFile;
private Context context;
-
+
public static interface RecognizerApiListener {
public void onSpeechResult(String result);
public void onSpeechError(int error);
}
-
+
private RecognizerApiListener mListener;
-
+
public RecognizerApi(Context context) {
this.context = context;
File dir = context.getFilesDir();
aacFile = dir.toString() + "/audio.aac";
-
+
sr = SpeechRecognizer.createSpeechRecognizer(context);
}
-
+
public void setTemporaryFile(String fileName) {
aacFile = context.getFilesDir().toString() + "/" + fileName;
}
-
+
public String getTemporaryFile() {
return aacFile;
}
-
+
public void setListener(RecognizerApiListener listener) {
this.mListener = listener;
}
@@ -78,20 +75,28 @@ public static void play(Activity activity, String file, PlaybackExceptionHandler
private SpeechRecognizer sr;
private ProgressDialog speakPd;
private ProgressDialog processingPd;
-
- public void start() {
+ private String processingMessage;
+
+ /**
+ * Start speech recognition
+ *
+ * @param callingPackage e.g. com.myapp.example
+ * @param speakNowMessage e.g. "Speak now!"
+ * @param processingMessage e.g. "Processing..."
+ */
+ public void start(String callingPackage, String speakNowMessage, String processingMessage) {
sr.setRecognitionListener(this);
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, "com.domain.app");
+ intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, callingPackage);
speechStarted = 0;
baos.reset();
speakPd = new ProgressDialog(context);
- speakPd.setMessage("Speak now...");
+ speakPd.setMessage(speakNowMessage);
speakPd.setIndeterminate(true);
speakPd.setCancelable(true);
speakPd.setOnCancelListener(new OnCancelListener() {
@@ -108,34 +113,31 @@ public void onCancel(DialogInterface dialog) {
speechStarted = System.currentTimeMillis();
}
- public void convert(String toFile) {
- try {
- new AACToM4A().convert(context, aacFile, toFile);
-
- Toast.makeText(context, "File Saved!", Toast.LENGTH_LONG).show();
- } catch (IOException e) {
- Toast.makeText(context, "Error :(", Toast.LENGTH_LONG).show();
- Log.e("ERROR", "error converting", e);
- }
+ /**
+ * Convert AAC file to M4A
+ *
+ * @param toFile
+ * @throws IOException
+ */
+ public void convert(String toFile) throws IOException {
+ new AACToM4A().convert(context, aacFile, toFile);
}
public void cancel() {
sr.cancel();
}
-
+
public void destroy() {
sr.setRecognitionListener(null);
sr.destroy();
}
-
+
// --- RecognitionListener methods --- //
-
+
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
@Override
public void onBeginningOfSpeech() {
- System.err.println("beginning");
-
}
@Override
@@ -155,9 +157,9 @@ public void onEndOfSpeech() {
if(speechStarted == 0)
return;
-
+
processingPd = new ProgressDialog(context);
- processingPd.setMessage("Processing...");
+ processingPd.setMessage(processingMessage);
processingPd.setIndeterminate(true);
processingPd.setCancelable(true);
processingPd.setOnCancelListener(new OnCancelListener() {
@@ -174,14 +176,10 @@ public void onCancel(DialogInterface dialog) {
sampleRate = 8000; // THIS IS A MAGIC NUMBER@?!!?!?!
// can i has calculate?
- System.err.println("computed sample rate: " + sampleRate);
-
encoder.init(64000, 1, sampleRate, 16, aacFile);
encoder.encode(baos.toByteArray());
- System.err.println("end");
-
encoder.uninit();
}
@@ -217,5 +215,5 @@ public void onResults(Bundle results) {
@Override
public void onRmsChanged(float arg0) {
}
-
+
}
@@ -5,8 +5,10 @@
*/
package com.todoroo.astrid.voice;
+import java.io.IOException;
import java.util.List;
+import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -16,14 +18,18 @@
import android.support.v4.app.Fragment;
import android.widget.EditText;
import android.widget.ImageButton;
+import android.widget.Toast;
import com.timsu.astrid.R;
+import com.todoroo.aacenc.ContextManager;
import com.todoroo.aacenc.RecognizerApi;
import com.todoroo.aacenc.RecognizerApi.RecognizerApiListener;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
+import com.todoroo.astrid.utility.Constants;
+@TargetApi(8)
public class VoiceRecognizer {
protected RecognizerApi recognizerApi;
@@ -79,7 +85,9 @@ public static VoiceRecognizer instantiateVoiceRecognizer(Context context, Recogn
public void startVoiceRecognition(Context context, String currentVoiceFile) {
if (speechRecordingAvailable(context) && recognizerApi != null) {
recognizerApi.setTemporaryFile(currentVoiceFile);
- recognizerApi.start();
+ recognizerApi.start(Constants.PACKAGE,
+ context.getString(R.string.audio_speak_now),
+ context.getString(R.string.audio_encoding));
} else {
int prompt = R.string.voice_edit_title_prompt;
if (Preferences.getBoolean(R.string.p_voiceInputCreatesTask, false))
@@ -107,7 +115,13 @@ public void cancel() {
}
public void convert(String filePath) {
- if (instance != null && instance.recognizerApi != null)
- instance.recognizerApi.convert(filePath);
+ if (instance != null && instance.recognizerApi != null) {
+ try {
+ instance.recognizerApi.convert(filePath);
+ } catch (IOException e) {
+ Toast.makeText(ContextManager.getContext(), R.string.audio_err_encoding,
+ Toast.LENGTH_LONG).show();
+ }
+ }
}
}

0 comments on commit 62e493b

Please sign in to comment.