如何同时执行语音识别和录音?

时间:2016-02-03 10:09:01

标签: android cordova speech-recognition voice-recognition android-mediarecorder

这不是一个重复,我在这里谈论停止并开始记录过程,无论我想要什么。在标记为重复之前,请仔细阅读其他答案。

我正在为Phonegap开发一个Android插件。这个插件基本上支持Android Speech Recognition并录制语音。我能够启动,停止等语音识别,但是录制时出现严重问题。首先,代码发布在下面。

以下是我如何开始演讲,结束演讲等等。

    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;
}

以下是我录制演讲的代码。这是一个分开的课程。

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;
    }


}

现在,有一个问题。启动识别服务时,如果我没有使用下面的代码,那么语音识别工作正常。

AudioRecorder recorder = AudioRecorder.getInstance();
recorder.startRecording();

但是如果我使用上面的代码,那么我会收到以下错误

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

基本上,如果我执行录制过程,则识别过程会出错。请问这里发生了什么?

在录音过程中,我必须制作用户可以随时开始录音的功能,并在他喜欢的任何时间停止录音。

0 个答案:

没有答案