MediaRecorder在Start()

时间:2016-07-15 19:14:26

标签: c# android xamarin xamarin.android

我在Android Xamarin中使用MediaRecorder时遇到了问题。 当我尝试启动或停止记录时,会引发此异常:

07-15 21:06:40.984 W/System.err(13609): java.lang.IllegalStateException
07-15 21:06:41.014 W/System.err(13609):     at android.media.MediaRecorder.start(Native Method)
07-15 21:06:41.014 W/System.err(13609):     at md51ef13e2ce92dda6cb40f015673d2b702.VideoAnswerActivity.n_BtnTakeVideo(Native Method)
07-15 21:06:41.014 W/System.err(13609):     at md51ef13e2ce92dda6cb40f015673d2b702.VideoAnswerActivity.BtnTakeVideo(VideoAnswerActivity.java:39)
07-15 21:06:41.014 W/System.err(13609):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 21:06:41.014 W/System.err(13609):     at java.lang.reflect.Method.invoke(Method.java:515)
07-15 21:06:41.024 W/System.err(13609):     at android.view.View$1.onClick(View.java:3955)
07-15 21:06:41.024 W/System.err(13609):     at android.view.View.performClick(View.java:4575)
07-15 21:06:41.024 W/System.err(13609):     at android.view.View$PerformClick.run(View.java:18578)
07-15 21:06:41.024 W/System.err(13609):     at android.os.Handler.handleCallback(Handler.java:733)
07-15 21:06:41.024 W/System.err(13609):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-15 21:06:41.024 W/System.err(13609):     at android.os.Looper.loop(Looper.java:136)
07-15 21:06:41.024 W/System.err(13609):     at android.app.ActivityThread.main(ActivityThread.java:5127)
07-15 21:06:41.024 W/System.err(13609):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 21:06:41.024 W/System.err(13609):     at java.lang.reflect.Method.invoke(Method.java:515)
07-15 21:06:41.024 W/System.err(13609):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
07-15 21:06:41.024 W/System.err(13609):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
07-15 21:06:41.024 W/System.err(13609):     at dalvik.system.NativeStart.main(Native Method)

我只是按照Xamarin教程录制视频: https://developer.xamarin.com/recipes/android/media/video/record_video/

这里是Start()的代码:

                recording = true;
                recorder.SetVideoSource(VideoSource.Camera);
                recorder.SetAudioSource(AudioSource.Mic);
                recorder.SetOutputFormat(OutputFormat.Default);
                recorder.SetVideoEncoder(VideoEncoder.Default);
                recorder.SetAudioEncoder(AudioEncoder.Default);
                recorder.SetOutputFile(path);
                recorder.SetPreviewDisplay(video.Holder.Surface);
                recorder.SetMaxDuration(30000);
                recorder.SetOrientationHint(90);
                recorder.Prepare();
                try
                {
                    recorder.Start();
                }
                catch (IllegalStateException e)
                {
                    // TODO Auto-generated catch block
                    e.PrintStackTrace();
                }

1 个答案:

答案 0 :(得分:2)

从代码中删除.Reset,因为重启/重置MediaRecorder,您必须重新启动它。

  

将MediaRecorder重新启动到空闲状态。在调用此方法之后,您将不得不再次对其进行配置,就像刚刚构建它一样。

参考:https://developer.xamarin.com/api/member/Android.Media.MediaRecorder.Reset()/

enter image description here