5
5
import java .io .IOException ;
6
6
import java .util .ArrayList ;
7
7
8
+ import android .annotation .TargetApi ;
8
9
import android .app .Activity ;
9
10
import android .app .ProgressDialog ;
10
11
import android .content .Context ;
11
12
import android .content .DialogInterface ;
12
13
import android .content .DialogInterface .OnCancelListener ;
13
14
import android .content .Intent ;
14
15
import android .media .MediaPlayer ;
15
- import android .net .Uri ;
16
16
import android .os .Bundle ;
17
17
import android .speech .RecognitionListener ;
18
18
import android .speech .RecognizerIntent ;
19
19
import android .speech .SpeechRecognizer ;
20
20
import android .util .Log ;
21
- import android .view .View ;
22
- import android .view .View .OnClickListener ;
23
- import android .widget .TextView ;
24
- import android .widget .Toast ;
25
21
22
+ @ TargetApi (8 )
26
23
public class RecognizerApi implements RecognitionListener {
27
24
28
25
public static interface PlaybackExceptionHandler {
29
26
public void playbackFailed (String file );
30
27
}
31
-
28
+
32
29
private String aacFile ;
33
30
34
31
private Context context ;
35
-
32
+
36
33
public static interface RecognizerApiListener {
37
34
public void onSpeechResult (String result );
38
35
public void onSpeechError (int error );
39
36
}
40
-
37
+
41
38
private RecognizerApiListener mListener ;
42
-
39
+
43
40
public RecognizerApi (Context context ) {
44
41
this .context = context ;
45
42
46
43
File dir = context .getFilesDir ();
47
44
aacFile = dir .toString () + "/audio.aac" ;
48
-
45
+
49
46
sr = SpeechRecognizer .createSpeechRecognizer (context );
50
47
}
51
-
48
+
52
49
public void setTemporaryFile (String fileName ) {
53
50
aacFile = context .getFilesDir ().toString () + "/" + fileName ;
54
51
}
55
-
52
+
56
53
public String getTemporaryFile () {
57
54
return aacFile ;
58
55
}
59
-
56
+
60
57
public void setListener (RecognizerApiListener listener ) {
61
58
this .mListener = listener ;
62
59
}
@@ -78,20 +75,28 @@ public static void play(Activity activity, String file, PlaybackExceptionHandler
78
75
private SpeechRecognizer sr ;
79
76
private ProgressDialog speakPd ;
80
77
private ProgressDialog processingPd ;
81
-
82
- public void start () {
78
+ private String processingMessage ;
79
+
80
+ /**
81
+ * Start speech recognition
82
+ *
83
+ * @param callingPackage e.g. com.myapp.example
84
+ * @param speakNowMessage e.g. "Speak now!"
85
+ * @param processingMessage e.g. "Processing..."
86
+ */
87
+ public void start (String callingPackage , String speakNowMessage , String processingMessage ) {
83
88
sr .setRecognitionListener (this );
84
89
85
90
Intent intent = new Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH );
86
91
intent .putExtra (RecognizerIntent .EXTRA_LANGUAGE_MODEL ,
87
92
RecognizerIntent .LANGUAGE_MODEL_FREE_FORM );
88
- intent .putExtra (RecognizerIntent .EXTRA_CALLING_PACKAGE , "com.domain.app" );
93
+ intent .putExtra (RecognizerIntent .EXTRA_CALLING_PACKAGE , callingPackage );
89
94
90
95
speechStarted = 0 ;
91
96
baos .reset ();
92
97
93
98
speakPd = new ProgressDialog (context );
94
- speakPd .setMessage ("Speak now..." );
99
+ speakPd .setMessage (speakNowMessage );
95
100
speakPd .setIndeterminate (true );
96
101
speakPd .setCancelable (true );
97
102
speakPd .setOnCancelListener (new OnCancelListener () {
@@ -108,34 +113,31 @@ public void onCancel(DialogInterface dialog) {
108
113
speechStarted = System .currentTimeMillis ();
109
114
}
110
115
111
- public void convert (String toFile ) {
112
- try {
113
- new AACToM4A ().convert (context , aacFile , toFile );
114
-
115
- Toast .makeText (context , "File Saved!" , Toast .LENGTH_LONG ).show ();
116
- } catch (IOException e ) {
117
- Toast .makeText (context , "Error :(" , Toast .LENGTH_LONG ).show ();
118
- Log .e ("ERROR" , "error converting" , e );
119
- }
116
+ /**
117
+ * Convert AAC file to M4A
118
+ *
119
+ * @param toFile
120
+ * @throws IOException
121
+ */
122
+ public void convert (String toFile ) throws IOException {
123
+ new AACToM4A ().convert (context , aacFile , toFile );
120
124
}
121
125
122
126
public void cancel () {
123
127
sr .cancel ();
124
128
}
125
-
129
+
126
130
public void destroy () {
127
131
sr .setRecognitionListener (null );
128
132
sr .destroy ();
129
133
}
130
-
134
+
131
135
// --- RecognitionListener methods --- //
132
-
136
+
133
137
private ByteArrayOutputStream baos = new ByteArrayOutputStream ();
134
138
135
139
@ Override
136
140
public void onBeginningOfSpeech () {
137
- System .err .println ("beginning" );
138
-
139
141
}
140
142
141
143
@ Override
@@ -155,9 +157,9 @@ public void onEndOfSpeech() {
155
157
156
158
if (speechStarted == 0 )
157
159
return ;
158
-
160
+
159
161
processingPd = new ProgressDialog (context );
160
- processingPd .setMessage ("Processing..." );
162
+ processingPd .setMessage (processingMessage );
161
163
processingPd .setIndeterminate (true );
162
164
processingPd .setCancelable (true );
163
165
processingPd .setOnCancelListener (new OnCancelListener () {
@@ -174,14 +176,10 @@ public void onCancel(DialogInterface dialog) {
174
176
sampleRate = 8000 ; // THIS IS A MAGIC NUMBER@?!!?!?!
175
177
// can i has calculate?
176
178
177
- System .err .println ("computed sample rate: " + sampleRate );
178
-
179
179
encoder .init (64000 , 1 , sampleRate , 16 , aacFile );
180
180
181
181
encoder .encode (baos .toByteArray ());
182
182
183
- System .err .println ("end" );
184
-
185
183
encoder .uninit ();
186
184
}
187
185
@@ -217,5 +215,5 @@ public void onResults(Bundle results) {
217
215
@ Override
218
216
public void onRmsChanged (float arg0 ) {
219
217
}
220
-
218
+
221
219
}
0 commit comments