From 6c4c94b935d0594257cfda7b693c3204f065c1cf Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Mon, 5 Feb 2024 22:00:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E9=80=9A=E9=81=93?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xypower/mpapp/video/VideoFragment.java | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java b/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java index 5331c3a6..5c083535 100644 --- a/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java +++ b/app/src/main/java/com/xypower/mpapp/video/VideoFragment.java @@ -20,6 +20,7 @@ import android.hardware.camera2.CameraManager; import android.hardware.camera2.CameraMetadata; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.params.StreamConfigurationMap; +import android.media.CamcorderProfile; import android.media.MediaRecorder; import android.os.Bundle; @@ -96,7 +97,7 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0); } - private String mCameraId; + private int mCameraId; private long mVideoId = 0; private int mDuration = 0; @@ -350,7 +351,7 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med Bundle argument = getArguments(); if (argument != null) { - mCameraId = Integer.toString(argument.getInt("CameraId", 0)); + mCameraId = argument.getInt("cameraId", 0); mVideoId = argument.getLong("videoId", 0); mDuration = argument.getInt("duration", 0); int width = argument.getInt("width", 0); @@ -491,10 +492,8 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { throw new RuntimeException("Time out waiting to lock camera opening."); } - String cameraId = manager.getCameraIdList()[0]; - // Choose the sizes for camera preview and video recording - CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); + CameraCharacteristics characteristics = manager.getCameraCharacteristics(Integer.toString(mCameraId)); StreamConfigurationMap map = characteristics .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); @@ -514,7 +513,7 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med configureTransform(width, height); mMediaRecorder = new MediaRecorder(); mMediaRecorder.setOnInfoListener(this); - manager.openCamera(cameraId, mStateCallback, null); + manager.openCamera(Integer.toString(mCameraId), mStateCallback, null); } catch (CameraAccessException e) { // Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show(); activity.finish(); @@ -639,11 +638,33 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med mTextureView.setTransform(matrix); } + public static int videoSizeToQuality(int width, int height) { + int quality = CamcorderProfile.QUALITY_480P; + if (height >= 1080) { + quality = CamcorderProfile.QUALITY_1080P; + } else if (height >= 720) { + quality = CamcorderProfile.QUALITY_720P; + } else if (height >= 480) { + quality = (width > 640) ? CamcorderProfile.QUALITY_480P : CamcorderProfile.QUALITY_VGA; + } else if (height >= 240) { + quality = CamcorderProfile.QUALITY_QVGA; + } + + return quality; + } + private void setUpMediaRecorder() throws IOException { final Activity activity = getActivity(); if (null == activity) { return; } + + CamcorderProfile camcorderProfile = null; + int quality = videoSizeToQuality(mVideoSize.getWidth(), mVideoSize.getHeight()); + if (CamcorderProfile.hasProfile(mCameraId, quality)) { + camcorderProfile = CamcorderProfile.get(mCameraId, quality); + } + mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); @@ -651,8 +672,9 @@ public class VideoFragment extends Fragment implements View.OnClickListener, Med mNextVideoAbsolutePath = getVideoFilePath(getActivity()); } mMediaRecorder.setOutputFile(mNextVideoAbsolutePath); - mMediaRecorder.setVideoEncodingBitRate(1024 * 1024 * 8); - mMediaRecorder.setVideoFrameRate(30); + mMediaRecorder.setVideoEncodingBitRate(camcorderProfile != null ? camcorderProfile.videoBitRate : (1024 * 1024 * 16)); + mMediaRecorder.setCaptureRate(camcorderProfile != null ? camcorderProfile.videoFrameRate : 30); + mMediaRecorder.setVideoFrameRate(camcorderProfile != null ? camcorderProfile.videoFrameRate : 30); mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);