java.io.IOException:无法开始录制

时间:2013-12-24 06:00:45

标签: android

我在我的应用程序中使用语音搜索功能。为此,我曾经使用Intent开始语音识别器: -

iSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
iSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
iSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");      
iSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

                                  sr.startListening(iSpeechIntent);

现在我遇到了这个例外:

-ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|Caused by: java.io.IOException: couldn't start recording
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at java.io.BufferedInputStream.read(BufferedInputStream.java:324)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at com.google.android.voicesearch.speechservice.RecognitionControllerImpl$1.handleMessage(RecognitionControllerImpl.java:251)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at android.os.HandlerThread.run(HandlerThread.java:60)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at android.os.Looper.loop(Looper.java:123)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at com.google.android.voicesearch.speechservice.RecognitionControllerImpl.recordAndSend(RecognitionControllerImpl.java:522)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at android.media.AmrInputStream.read(AmrInputStream.java:88)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at com.google.android.voicesearch.speechservice.RecognitionControllerImpl.access$100(RecognitionControllerImpl.java:82)
ERROR|12-11 18:56:44.035|4053|4067||RecognitionControllerImpl|  at com.google.android.voicesearch.speechservice.AudioBuffer.access$000(AudioBuffer.java:34)

我的问题是如何使用未捕获的异常句柄处理此异常?

2 个答案:

答案 0 :(得分:0)

更改ecognizerIntent.LANGUAGE_MODEL_FREE_FORM

进入RecognizerIntent.LANGUAGE_MODEL_FREE_FORM

可能这会导致错误。

删除以下行 -

sr.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));

答案 1 :(得分:0)

如果你想要一个UncaughtExceptionHandler,HTH:

private UnexpectedTerminationStuff mUnexpectedTerminationStuff = new UnexpectedTerminationStuff();
private class UnexpectedTerminationStuff {
    private Thread mThread;
    private Thread.UncaughtExceptionHandler mOldUncaughtExceptionHandler = null;
    private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            // gets handled in the same thread (main)
            Log.i(TAG, "uncaught exception in the thread '"+ thread.getName()+ "' (id="+thread.getId() + "), closing the camera");
            Log.i(TAG, "handled in the thread '"+ Thread.currentThread().getName()+ "' (id="+Thread.currentThread().getId() + ")");
            ex.printStackTrace();

            // .....
            // CLOSE SOMETHING
            // ......

            Log.i(TAG, "going to execute the previous handler: "+mOldUncaughtExceptionHandler);
            if(mOldUncaughtExceptionHandler != null) {
                // it displays the "force close" dialog
                mOldUncaughtExceptionHandler.uncaughtException(thread, ex);
            }
        }
    };
    void init() {
        mThread = Thread.currentThread();
        UncaughtExceptionHandler oldUncaughtExceptionHandler = mThread.getUncaughtExceptionHandler();
        if (oldUncaughtExceptionHandler != mUncaughtExceptionHandler) {
            mOldUncaughtExceptionHandler = oldUncaughtExceptionHandler; 
            mThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler);
            Log.d(TAG,"~~~ UnexpectedTermination init stuff, doing setUncaughtExceptionHandler");
        } else {
            Log.d(TAG,"~~~ UnexpectedTermination init stuff, skipping setUncaughtExceptionHandler");
        }
        Log.d(TAG,"~~~ UnexpectedTermination init stuff, mThread="+mThread+" mOldUncaughtExceptionHandler="+mOldUncaughtExceptionHandler);
    }
    void fini() {
        Log.d(TAG,"~~~ UnexpectedTermination windup stuff, mThread="+mThread+" mOldUncaughtExceptionHandler="+mOldUncaughtExceptionHandler);
        if (mThread != null) {
            mThread.setUncaughtExceptionHandler(mOldUncaughtExceptionHandler);
            mOldUncaughtExceptionHandler = null;
            mThread = null;
        }
    }
}

要使用它,请在适当的位置拨打mUnexpectedTerminationStuff.init()mUnexpectedTerminationStuff.fini(),并将“CLOSE SOMETHING”评论替换为有用的内容。