Android媒体录制器启动失败

时间:2017-05-25 10:34:48

标签: android android-7.1-nougat

我正在使用以下代码在我的应用程序中录制音频

private void startRecording() {

    if (mRecorder == null) {
        mRecorder = new MediaRecorder();
    }

    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(path);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.i(TAG, e.getMessage());
    }
    mRecorder.start();

}

上面的代码完全适用于android lollipop 5.1但在android nougat 7.1.1上获得以下错误

05-25 14:51:45.245 21686-21686/com.chatting.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.chatting.test, PID: 21686
                                                                java.lang.RuntimeException: start failed.
                                                                    at android.media.MediaRecorder.start(Native Method)
                                                                    at com.chatting.test.Activities.Chats.PrivateChatActivity.startRecording(PrivateChatActivity.java:1363)
                                                                    at com.chatting.test.Activities.Chats.PrivateChatActivity.onTouch(PrivateChatActivity.java:219)
                                                                    at android.view.View.dispatchTouchEvent(View.java:10019)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
                                                                    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
                                                                    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
                                                                    at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
                                                                    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                                                                    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
                                                                    at android.view.View.dispatchPointerEvent(View.java:10243)
                                                                    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
                                                                    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
                                                                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
                                                                    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
                                                                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
                                                                    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
                                                                    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
                                                                    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349)
                                                                    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                    at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                    at android.os.MessageQueue.next(MessageQueue.java:323)
                                                                    at android.os.Looper.loop(Looper.java:136)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我已经在nougat 7.1.1

上授予了运行时以下权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如何让上面的代码与android nougat 7.1.1一起工作?

2 个答案:

答案 0 :(得分:-2)

试试这个希望它会起作用:)

  private void startRecording(){
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        AppLog.logString("Error: " + what + ", " + extra);
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        AppLog.logString("Warning: " + what + ", " + extra);
    }
};

private void stopRecording(){
    if(null != recorder){
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}

答案 1 :(得分:-3)

设置源代码后尝试将CamcorderProfile设置为以下代码:

 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
 mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
 mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

来自https://developer.android.com/guide/topics/media/camera.html#capture-video

的参考资料