gpt4 book ai didi

android - 尝试从空对象引用的字段 'int android.media.CamcorderProfile.fileFormat' 读取

转载 作者:行者123 更新时间:2023-11-29 20:32:35 26 4
gpt4 key购买 nike

我在 Nexus 7 2012 上使用自定义摄像头录制视频时遇到了这个问题,而其他设备,如 Zenfone 2、Galaxy S3、S6、HTC One M7、Oppo N1、Sony Xperia M4 ... 运行良好。

public static boolean prepareVideoRecorder(Context mContext, int mode) {
// Should release before use new Preview for Recording Video mode
CustomCamera.releaseCamera();

// Initialize camera
CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);

CustomCamera.mCamera.startPreview();

// Set orientation display
setCameraDisplayOrientation((Activity) mContext, mode);

// Should release before use new Preview for Recording Video mode
CustomCamera.releaseMediaRecorder();

CustomCamera.mMediaRecorder = new MediaRecorder();

// Step 1: Unlock and set camera to MediaRecorder
CustomCamera.mCamera.unlock();
CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);

// Step 2: Set sources
CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
// todo NullPointerException: Attempt to read from field 'int android.media.CamcorderProfile.fileFormat' on a null object reference
//

// returned true value, go inside
if (CamcorderProfile.hasProfile(current_camera_id, CamcorderProfile.QUALITY_LOW)) {
// CamcorderProfile.get(CamcorderProfile.QUALITY_LOW) RETURNED NULL VALUE, SO I CAN NOT SET PROFILE
CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

// Step 4: Set output file - pass False value to indicate choose Video mode
RECORDED_FILE_PATH = getOutputMediaFile(mContext, false).toString();
CustomCamera.mMediaRecorder.setOutputFile(RECORDED_FILE_PATH);

// Step 5: Set the preview output
/**
* Define Orientation of image in here,
* if in portrait mode, use value = 90,
* if in landscape mode, use value = 0
*/
if (current_camera_id == define.Camera.CAMERA_BACK)
// Back Camera
CustomCamera.mMediaRecorder = Utils.rotateBackVideo(CustomCamera.mMediaRecorder);
else
// Front Camera
CustomCamera.mMediaRecorder = Utils.rotateFrontVideo(CustomCamera.mMediaRecorder);

// Set preview display to refresh current screen
CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

// Step 6: Prepare configured MediaRecorder
try {
CustomCamera.mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
CustomCamera.releaseMediaRecorder();
return false;
} catch (IOException e) {
e.printStackTrace();
CustomCamera.releaseMediaRecorder();
return false;
}

return true;
}

我设置的这些权限:

<uses-feature android:name="android.hardware.camera.front" />
<uses-feature android:name="android.hardware.microphone"/>

<uses-permission android:name = "android.permission.CAMERA" />
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name = "android.permission.RECORD_AUDIO" />
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name = "android.permission.WRITE_INTERNAL_STORAGE" />

我想要的是让它可以在 Nexus 7 2012 上录制视频。

认识的人,

请告诉我如何修复。

谢谢,

p/s :如果我删除 setProfile() 行,我会得到另一个异常并且无法录制视频:

07-31 14:33:39.936  25960-25960/com.panasonic_Upload E/MediaRecorder﹕ setOutputFile called in an invalid state(2)
07-31 14:33:39.942 25960-25960/com.panasonic_Upload W/System.err﹕ java.lang.IllegalStateException
07-31 14:33:39.950 25960-25960/com.panasonic_Upload W/System.err﹕ at android.media.MediaRecorder._setOutputFile(Native Method)
07-31 14:33:39.950 25960-25960/com.panasonic_Upload W/System.err﹕ at android.media.MediaRecorder.prepare(MediaRecorder.java:740)
07-31 14:33:39.950 25960-25960/com.panasonic_Upload W/System.err﹕ at ui.fragment.camera.CameraPreviewFragment.prepareVideoRecorder(CameraPreviewFragment.java:533)
07-31 14:33:39.950 25960-25960/com.panasonic_Upload W/System.err﹕ at ui.fragment.camera.CameraPreviewFragment.onClick(CameraPreviewFragment.java:223)
07-31 14:33:39.950 25960-25960/com.panasonic_Upload W/System.err﹕ at android.view.View.performClick(View.java:4780)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at android.view.View$PerformClick.run(View.java:19866)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-31 14:33:39.951 25960-25960/com.panasonic_Upload W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-31 14:33:39.952 25960-25960/com.panasonic_Upload W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-31 14:33:39.952 25960-25960/com.panasonic_Upload W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

调用 get() 时必须传递相机 ID,与调用 hasProfile() 时完全相同。否则,它将从它找到的第一个后置摄像头获取配置文件。

http://developer.android.com/reference/android/media/CamcorderProfile.html#pubmethods

Nexus 7 2012 崩溃是因为它没有后置摄像头。

您需要这样做:

CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(<strong>current_camera_id</strong>, CamcorderProfile.QUALITY_LOW));

关于android - 尝试从空对象引用的字段 'int android.media.CamcorderProfile.fileFormat' 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com