![](/img/trans.png)
[英]Media Recorder Start Failed -19 on Nexus 9 Android Lollipop 5.1.1
[英]Media recorder setprofile() is not working for nexus phone
我已經在Samsung平板電腦上測試了我的代碼,並注意,但是相同的代碼在nexus上不起作用。
我知道連結只有前置攝像頭,並且攝像頭正在打開,但是在開始錄制按鈕上單擊它會崩潰。...我通過setProfile上的錯誤檢查了它。
我的密碼:
public boolean prepareVideoRecorder() {
Toast.makeText(context, "Please wait..", Toast.LENGTH_SHORT).show();
// mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.stopPreview();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
int layout = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
if (layout == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
// Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
int degrees = 0;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
// Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 1111", Toast.LENGTH_SHORT).show();
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
// Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 222-- "+ result, Toast.LENGTH_SHORT).show();
}
mMediaRecorder.setOrientationHint(result);
break;
}
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
// Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
int degrees = 270;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
// Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 111", Toast.LENGTH_SHORT).show();
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
// System.out.println("FRONT CAMERA )))))))");
} else { // back-facing
// Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 222", Toast.LENGTH_SHORT).show();
result = degrees;// (info.orientation - degrees + 360) % 360;
// System.out.println("BACK CAMERA )))))))");
}
mMediaRecorder.setOrientationHint(result);
break;
}
case Configuration.ORIENTATION_LANDSCAPE: {
// Toast.makeText(context, "ORIENTATION_LANDSCAPE", Toast.LENGTH_LONG).show();
int degrees = 180;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
// Toast.makeText(context, "ORIENTATION_LANDSCAPE 111", Toast.LENGTH_SHORT).show();
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
// Toast.makeText(context, "ORIENTATION_LANDSCAPE 22", Toast.LENGTH_SHORT).show();
result = (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
break;
}
default: {
//Toast.makeText(context, "DEFAULT 11", Toast.LENGTH_SHORT).show();
int degrees = 90;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = degrees;// (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
}
}
}else if (layout == Configuration.SCREENLAYOUT_SIZE_LARGE) {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show();
int degrees = 0;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show();
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show();
}
mMediaRecorder.setOrientationHint(result);
break;
}
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show();
int degrees = 90;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = degrees;// (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
break;
}
}
}else
{
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show();
int degrees = 0;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show();
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show();
}
mMediaRecorder.setOrientationHint(result);
break;
}
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show();
int degrees = 90;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = degrees;// (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
break;
}
}
}
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CamcorderProfile camcorderProfile_HQ;
if (CurrentValues.cur_cam_face == 1)
camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
else
camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setProfile(camcorderProfile_HQ);
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO)
.toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG,
"IllegalStateException preparing MediaRecorder: "
+ e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
假設你想在最新Nexus 7
,我覺得這個問題可能是QUALITY_HIGH
未在規定 media_profiles.xml
您的設備。 我在這里檢查了AOSP
樹,發現僅定義了CIF
, QCIF
, 480p
及其延時版本。 您可以通過在media_profiles.xml
上查看設備上的/etc/media_profiles.xml
來確認。
您可以修改代碼以使用CamcorderProfile.QUALITY_480P
而不是CamcorderProfile.QUALITY_HIGH
。
我看到您的代碼有幾個問題,這兩個問題都可能導致您的錯誤。
首先,僅創建CameraInfo對象是不夠的,您必須調用:
Camera.getCameraInfo(cameraId, info);
沒有此調用,您將始終僅獲得0的朝向和方向(默認整數值)。
其次,您還應該使用接受cameraId的方法來獲取配置文件:
CamcorderProfile.get(cameraId, quality);
我假設您正確使用了Camera.open(int cameraId)方法,或者您可能沒有使任何前置攝像頭正常工作。
另外,這只是約定,但我建議您將switch語句中的方向代碼分解為便於閱讀的函數。 理想情況下,開關盒應該只有一條線,但是幾行仍然可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.