问题
MediaRecorder is crashing my app when I set the output file to a custom directory. If I set it to
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
, it works completely fine.
My Code:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16*44100);
recorder.setAudioSamplingRate(44100);
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/My App/Voice/", new Date().getTime() + ".3gp");
recorder.setOutputFile(file.getAbsoluteFile());
I made sure that my microphone as well as my reading and writing in storage permissions are allowed. Any help would be appreciated.
Edit:
Error Log:
E/MediaRecorder: stop called in an invalid state: 4
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.IllegalStateException
at android.media.MediaRecorder._stop(Native Method)
at android.media.MediaRecorder.stop(MediaRecorder.java:1378)
at com.example.criminalscurse.Home$6.onTouch(Home.java:328)
at android.view.View.dispatchTouchEvent(View.java:13480)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at android.view.View.dispatchPointerEvent(View.java:13732)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6131)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5909)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5358)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5411)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5377)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5536)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5385)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5593)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5358)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5411)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5377)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5385)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5358)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8420)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8353)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8306)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8535)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.criminalscurse, PID: 5259
java.lang.IllegalStateException
at android.media.MediaRecorder._stop(Native Method)
at android.media.MediaRecorder.stop(MediaRecorder.java:1378)
at com.example.criminalscurse.Home$6.onTouch(Home.java:328)
at android.view.View.dispatchTouchEvent(View.java:13480)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at android.view.View.dispatchPointerEvent(View.java:13732)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6131)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5909)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5358)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5411)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5377)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5536)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5385)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5593)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5358)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5411)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5377)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5385)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5358)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8420)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8353)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8306)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8535)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
回答1:
Try whether this helps
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16*44100);
recorder.setAudioSamplingRate(44100);
file = new File(Environment.getExternalStorageDirectory(),"MyApp"+File.seperator+"Voice", new Date().getTime() + ".3gp");
recorder.setOutputFile(file.getAbsoluteFile());
来源:https://stackoverflow.com/questions/63098894/mediarecorder-crashing