问题
THIS IS NOT A DUPLICATE, HERE I TALK ABOUT STOPPING AND STARTING THE RECORD PROCESS WHENEVER I WANT. BEFORE MARKING AS DUPLICATES, PLEASE READ THE OTHER ANSWER PROPERLY.
I am developing a Phonegap
plugin for Android
. This plugin will basically support the Android Speech Recognition
and recording the speech. I am capable of starting, stopping etc the speech recognition, but got serious issues with recording. First, the code is posted below.
Below is how I start the speech, end it etc.
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException
{
final int duration = Toast.LENGTH_SHORT;
this.callBackContext = callbackContext;
// Shows a toast
Log.v(TAG,"CoolPlugin received:"+ action);
if(INITIALIZE.equals(action))
{
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
init();
}
});
}
else if(PLAY.equals(action))
{
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
//startRecording();
play(callbackContext);
timeMilli = System.currentTimeMillis();
timer();
AudioRecorder recorder = AudioRecorder.getInstance();
recorder.startRecording();
}
});
}
else if(STOP.equals(action))
{
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
stop();
AudioRecorder recorder = AudioRecorder.getInstance();
recorder.stopRecording();
}
});
}
return true;
}
public void play(CallbackContext callbackContext)
{
//Mute the sound
mAudioManager = (AudioManager) cordova.getActivity().getSystemService(Context.AUDIO_SERVICE) ;
mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); // getting system volume into var for later un-muting
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // setting system volume to
//Start listening
speech.stopListening();
speech.cancel();
speech.destroy();
createRecog();
speech.startListening(recognizerIntent);
//adapter.clear();
}
public String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
Below is my code for Recording the speech. It is a separated class.
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.File;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
public class AudioRecorder {
private static final String LOG_TAG = "AudioRecoder";
private MediaRecorder mRecorder;
String file;
public static AudioRecorder instance;
public static AudioRecorder getInstance()
{
if(instance==null)
{
instance = new AudioRecorder();
}
return instance;
}
private AudioRecorder()
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
// String myfile = path + "filename" + ".mp4";
file=path + "filename2" + ".3gp";
}
public void startRecording() {
new Thread(new Runnable(){
@Override
public void run() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(file);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
Log.e(LOG_TAG, "prepare() success");
} catch (IOException e) {
// Log.e(LOG_TAG, "prepare() failed");
Log.d(LOG_TAG, "prepare() failed", e);
}
mRecorder.start();
Log.e(LOG_TAG, "Working");
}}).start();
}
public void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
Now, there is an issue. When starting the recognition service, if I didn't use the below code, then speech recognition is working fine.
AudioRecorder recorder = AudioRecorder.getInstance();
recorder.startRecording();
However if I use the above code, then I get the following error
02-03 15:33:21.555: D/VoiceRecognitionActivity(32066): FAILED Network error
02-03 15:33:21.575: E/SpeechRecognizer(32066): not connected to the recognition service
02-03 15:33:21.575: E/SpeechRecognizer(32066): not connected to the recognition service
Basically speaking, if I execute the recording process, then the recognition process is giving errors. What is happening here please?
In the recording process I will have to make functions where the user can start the recording at anytime he wants and stop it at anytime he likes.
来源:https://stackoverflow.com/questions/35173978/how-to-execute-both-speech-recognition-and-audio-recording-at-the-same-time