视频录制无法使用摄像机在HTC One S上运行

时间:2014-09-17 14:13:44

标签: android android-camera video-capture android-video-player

实际上我需要用我的应用程序录制不同分辨率的视频(如480p,720p,1080p)。在HTC One S(Android 4.0.4)中,视频录制不适用于任何摄像机配置文件(除了QUALITY_LOW,QUALITY_CIF和QUALITY_QCIF,但录制的视频已损坏)。以下是我的代码段:

    mMediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mMediaRecorder.setProfile(CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH));

    mMediaRecorder.setOutputFile(getOutputMediaFile().toString());

    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

我还尝试手动设置MediaRecorder配置文件参数,但我只能用mMediaRecorder.setVideoSize(640,480)录制视频。使用其他值对(如(960,540)),我无法录制视频。

    mMediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);


    mMediaRecorder.setOutputFile(getOutputMediaFile().toString());

    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mMediaRecorder.setAudioEncodingBitRate(196608);

    mMediaRecorder.setVideoSize(640, 480);
    // mMediaRecorder.setVideoSize(1920, 1080); // Not Working
    // mMediaRecorder.setVideoSize(1280, 720); // Not Working
    // mMediaRecorder.setVideoSize(960, 540); // Not Working

    mMediaRecorder.setVideoFrameRate(16);
    mMediaRecorder.setVideoEncodingBitRate(3000000);

    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    mMediaRecorder.setOutputFile(getOutputMediaFile().toString());

    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

使用其他视频大小值(例如setVideoSize(1280,720)),我无法录制视频并获得RunTimeException。下面是我的stacktrace:

    09-17 19:29:22.431: E/MediaRecorder(11634): start failed: -19
    09-17 19:29:22.431: V/MediaRecorderJNI(11634): process_media_recorder_call
    09-17 19:29:22.431: E/MediaRecorder(11634): start failed.
    09-17 19:29:22.431: E/MediaRecorder(11634): try to delete broken file:    /mnt/sdcard  /Movies/MyApp/VID_20140917_192922.mp4
    09-17 19:29:22.431: D/AndroidRuntime(11634): Shutting down VM
    09-17 19:29:22.431: W/dalvikvm(11634): threadid=1: thread exiting with uncaught exception (group=0x40aaaa08)
    09-17 19:29:22.431: E/AndroidRuntime(11634): FATAL EXCEPTION: main
    09-17 19:29:22.431: E/AndroidRuntime(11634): java.lang.RuntimeException: start failed.
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.media.MediaRecorder._start(Native Method)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.media.MediaRecorder.start(MediaRecorder.java:770)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at com.collaaj.activity.camera.CameraActivity.captureVideo(CameraActivity.java:98)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at com.collaaj.activity.camera.CameraActivity.onClick(CameraActivity.java:68)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.view.View.performClick(View.java:3538)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.view.View$PerformClick.run(View.java:14319)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.os.Handler.handleCallback(Handler.java:608)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.os.Handler.dispatchMessage(Handler.java:92)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.os.Looper.loop(Looper.java:156)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at android.app.ActivityThread.main(ActivityThread.java:5045)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at java.lang.reflect.Method.invokeNative(Native Method)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at java.lang.reflect.Method.invoke(Method.java:511)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    09-17 19:29:22.431: E/AndroidRuntime(11634):    at dalvik.system.NativeStart.main(Native Method)

注意: 1)。每个可用分辨率的视频录制与HTC One S的原生相机应用程序一起正常工作 2)。所有摄像机配置文件的视频录制在三星Galaxy S3(Android 4.3),三星Galaxy S4(Android 4.4.2)和LG Nexus 4(Android 4.4)等其他设备上运行良好。

请指导我解决问题。任何帮助或指导都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我在HTC One上面临类似的挑战,必须首先设置CamCorderProfile QUALITY_LOW,然后覆盖某些设置:

CamcorderProfile profl = CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_LOW);
profl.videoFrameWidth = mOptimalSize.width;
profl.videoFrameHeight = mOptimalSize.height;
mRecorder.setProfile(profl);
mRecorder.setVideoFrameRate(25);
mRecorder.setVideoEncodingBitRate(3000000);

mOptimalSize设置如下:

private void setRecorderSize() {
    List<Size> validPreviewSizes = CameraUtils.getValidPreviewSizes(mCamera);       
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.set("cam_mode", 1);        

    //Camera.Size optimalSize =         
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    mOptimalSize = CameraPreview.getOptimalPreviewSize(validPreviewSizes, metrics.widthPixels, metrics.heightPixels);
    parameters.setPreviewSize(mOptimalSize.width, mOptimalSize.height);         
}