Failed to start Android media recorder

I am using below code to record audio in my application

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();

}

      

above code works fine on android lollipop 5.1 but gets below error in 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)

      

I have already provided below runtime permission on nougat 7.1.1

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

      

how can i make the code work with android nougat 7.1.1?

+3


source to share


2 answers


Try this hope it will work :)



  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


source


after installing source Try to set as code CamcorderProfile code:

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

      



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

-1


source







All Articles