0

我正在开发一个需要录制视频的应用程序

这是我的部分代码

mRecorder = new MediaRecorder();
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
String mFileName = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
mFileName += "/youraudiofile.3gp";
mSurfaceHolder.getSurface();
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mRecorder.setOutputFile(mFileName);
mRecorder.setVideoSize(320, 240);
mRecorder.setVideoFrameRate(15);
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setMaxDuration(30000);
try {
mRecorder.prepare();
mRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

上面的代码抛出了 IllegalStateException。我在 AndroidManifest.xml 中输入了所有权限,我非常确定 AndroidManifest.xml 没有任何问题。

我已经阅读了几个解决方案,但都没有工作。我怎么解决这个问题?

下面提供的日志猫

11-09 20:47:39.787: E/AndroidRuntime(29000): 致命异常: main
11-09 20:47:39.787: E/AndroidRuntime(29000): java.lang.IllegalStateException: 无法执行活动的方法
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.view.View$1.onClick(View.java:3624)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.view.View.performClick(View.java:4117)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.view.View$PerformClick.run(View.java:17052)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.os.Handler.handleCallback(Handler.java:615)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.os.Handler.dispatchMessage(Handler.java:92)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.os.Looper.loop(Looper.java:137)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.app.ActivityThread.main(ActivityThread.java:4812)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 java.lang.reflect.Method.invoke(Method.java:511)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-09 20:47:39.787: E/AndroidRuntime(29000): at dalvik.system.NativeStart.main(Native Method)
11-09 20:47:39.787:E/AndroidRuntime(29000):由:java.lang.reflect.InvocationTargetException 引起
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 java.lang.reflect.Method.invoke(Method.java:511)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 android.view.View$1.onClick(View.java:3619)
11-09 20:47:39.787: E/AndroidRuntime(29000): ... 11 更多
11-09 20:47:39.787:E/AndroidRuntime(29000):原因:java.lang.RuntimeException:启动失败。
11-09 20:47:39.787:E/AndroidRuntime(29000):在 android.media.MediaRecorder.start(本机方法)
11-09 20:47:39.787: E/AndroidRuntime(29000): 在 com.example.camera2.CameraView.startRecording(CameraView.java:149)
11-09 20:47:39.787: E/AndroidRuntime(29000): ... 14 更多

修改后的代码如下,即使 setOutputFormat(), setVideoSource(), setAudioSource() 出现在 if {} 的正上方, setProfile() 也会抛出 RunTimeException。下面的代码会抛出和之前的日志一样的 IllegalStateException

    mRecorder = new MediaRecorder();

    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    String mFileName = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    mFileName += "/youraudiofile.3gp";
    mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    } else {

        if (! isFrontCamera) {
            CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            mRecorder.setProfile(camcorderProfile);

        } else {
            CamcorderProfile camcorderProfile = CamcorderProfile.get(findFrontFacingCamera(), CamcorderProfile.QUALITY_LOW);
            mRecorder.setProfile(camcorderProfile);
        }

        mRecorder.setVideoSize(720, 480);
    }
    mRecorder.setVideoFrameRate(15);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setMaxDuration(30000);

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

2 回答 2

2

尝试删除此行并检查一次..

mRecorder.setVideoSize(320, 240);

您的某些设置在 3.0 之前不起作用,所以像这样设置它们..

 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {

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

    } else {

        if (! isFrontCamera) {
            CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            mMediaRecorder.setProfile(camcorderProfile);

        } else {
            CamcorderProfile camcorderProfile = CamcorderProfile.get(mFrontCamId, CamcorderProfile.QUALITY_LOW);
            mMediaRecorder.setProfile(camcorderProfile);
        }

        mMediaRecorder.setVideoSize(720, 480);
    }
于 2013-11-09T13:11:40.830 回答
0

您的清单中是否有这些权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" / >
于 2013-11-09T12:32:29.340 回答