【问题标题】:setVideoSource throwing RunTimeException (was: IllegalStateException)setVideoSource 抛出 RunTimeException(原为:IllegalStateException)
【发布时间】:2016-06-29 02:30:39
【问题描述】:

当我对 MediaRecorder 对象调用 setVideoSource 时,它会引发 IllegalStateException。我检查了这些:

(Android SDk 2.1) Getting error when I use setAudioSource and setVideoSource

Why following code shows setVideoSource failed exception setvideosource-failed-exception

MediaPlayer.prepare() throws IllegalStateException on Android L

但建议的解决方案要么没有帮助,要么不合适。

代码如下:

private void initRecorder() {
    Log.i(TAG, "InitRecorder");
    if(mRecorder == null) {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecorder.setVideoSize(640, 480);
        mRecorder.setVideoFrameRate(30);
        mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        mRecorder.setProfile(cpHigh);
        mRecorder.setOutputFile("/sdcard/video.mp4");
        mRecorder.setMaxDuration(50000); // 50 seconds
        mRecorder.setMaxFileSize(5000000); // Approximately 5 megabytes
    }
}

我确定 setOutputFormat 没有在 setVideoSource 之前被调用,正在请求和授予正确的权限,并且只存在一个 VideoRecorder 对象。

我在装有 Marshmallow 的 Moto G 上使用 Android SDK 23。

有人有什么想法吗?从异常中获取更多信息会很有用,但不知何故 Android Studio 不会让我了解它。我想一个相关的问题是如何在抛出异常时检查异常?它是否可能包含更多信息,而不仅仅是 IllegalStateException 的事实?

这个异常现在变成了一个RunTimeException,这让人很困惑。我已经取消了对它所做的任何更改,所以它应该抛出 IllegalStateException 但它现在几乎卡在了 RunTimeException 上。嗯……

这是错误日志:

06-28 13:10:14.989 30933-30933/com.dogtail W/ActivityThread: Application com.dogtail is waiting for the debugger on port 8100...
06-28 13:10:14.990 30933-30933/com.dogtail I/System.out: Sending WAIT chunk
06-28 13:10:15.142 30933-30939/com.dogtail I/art: Debugger is active
06-28 13:10:15.190 30933-30933/com.dogtail I/System.out: Debugger has connected
06-28 13:10:15.191 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:15.391 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:15.591 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:15.791 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:15.992 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:16.192 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:16.392 30933-30933/com.dogtail I/System.out: waiting for debugger to settle...
06-28 13:10:16.592 30933-30933/com.dogtail I/System.out: debugger has settled (1312)
06-28 13:10:16.636 30933-30933/com.dogtail W/System: ClassLoader referenced unknown path: /data/app/com.dogtail-1/lib/arm
06-28 13:10:17.599 30933-30933/com.dogtail W/System: ClassLoader referenced unknown path: /data/app/com.dogtail-1/lib/arm
06-28 13:10:17.681 30933-30933/com.dogtail I/APPLICATION: onCreate
06-28 13:10:17.934 30933-31061/com.dogtail I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/09/15, 6cbbf7d, I3193f6e94a
06-28 13:10:17.936 30933-31061/com.dogtail I/OpenGLRenderer: Initialized EGL, version 1.4
06-28 13:10:20.588 30933-30933/com.dogtail I/VideoRecorder: OnCreate
06-28 13:10:20.588 30933-30933/com.dogtail I/VideoRecorder: InitRecorder
06-28 13:10:20.598 30933-30933/com.dogtail E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.dogtail, PID: 30933
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dogtail/com.dogtail.VideoRecorder}: java.lang.RuntimeException: setVideoSource failed.
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                              Caused by: java.lang.RuntimeException: setVideoSource failed.
                                                                 at android.media.MediaRecorder.setVideoSource(Native Method)
                                                                 at com.dogtail.VideoRecorder.initRecorder(VideoRecorder.java:45)
                                                                 at com.dogtail.VideoRecorder.onCreate(VideoRecorder.java:30)
                                                                 at android.app.Activity.performCreate(Activity.java:6245)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

【问题讨论】:

  • 请添加您的错误日志

标签: java android android-camera


【解决方案1】:

您同时使用 setOutputFormat 和 setProfile。

删除下面一行

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);。

错误消失

你也应该删除行

mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

因为 mRecorder.setProfile(cpHigh);默认情况下已经添加了这些属性。

【讨论】:

  • 在 setOutputFormat 之前的 setVideoSource 行上引发了异常,所以我认为不是这样......
  • setOutputFormat 属性添加了两次,setProfile 也添加了此属性。 android媒体框架不太聪明
  • 但是在调用 setOutputFormat 之前抛出异常?
  • 我去掉了对setOutputFormat的调用,还是抛出了RunTimeException
  • 啊哈!是权限,对不起,权限.CAMERA 被错误地从权限列表中删除了。感谢您的帮助。
猜你喜欢
  • 2019-06-10
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
相关资源
最近更新 更多