问题
I've been having an issue using the camera 2 api for android. I'm able to record videos, but during playback, only the audio plays. After the video is done playing, the time jumps ahead anywhere from 10 minutes to 2 hours and then plays the video back. I've never heard of an issue like this. I pretty much followed this. Here is the code for setting my media recorder:
mMediaRecorder = new MediaRecorder();
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//limit the video duration
mMediaRecorder.setMaxDuration(90000); //90s
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaFile = getOutputMediaFile();
mMediaRecorder.setOutputFile(mMediaFile.toString());
mMediaRecorder.setVideoEncodingBitRate(1000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(),mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//since video is portrait, rotation is 90
if(!isFrontFacing) {
mMediaRecorder.setOrientationHint(270);
} else{
mMediaRecorder.setOrientationHint(90);
}
mMediaRecorder.prepare();
This is the code that starts the recording:
try {
closePreviewSession();
prepareVideoRecorder();
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(mPreviewSize.getWidth(),
mPreviewSize.getHeight());
mPreviewBuilder = mCameraDevice.createCaptureRequest
(CameraDevice.TEMPLATE_RECORD);
List<Surface> surfaces = new ArrayList<Surface>();
Surface previewSurface = new Surface(texture);
surfaces.add(previewSurface);
mPreviewBuilder.addTarget(previewSurface);
// Set up Surface for the MediaRecorder
mRecorderSurface = mMediaRecorder.getSurface();
surfaces.add(mRecorderSurface);
mPreviewBuilder.addTarget(mRecorderSurface);
// Start a capture session
// Once the session starts, we can update the UI and start recording
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
mPreviewSession = cameraCaptureSession;
updatePreview();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// UI
isRecording = true;
// Start recording
mMediaRecorder.start();
}
});
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Activity activity = getActivity();
if (null != activity) {
Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
}
}
}, mBackgroundHandler);
} catch (RuntimeException e) {
Toast.makeText(getContext(), "You're resolution isn't supported.", Toast
.LENGTH_SHORT).show();
getActivity().finish();
}
catch (CameraAccessException e) {
getActivity().finish();
}
来源:https://stackoverflow.com/questions/41931450/android-camera2-api-audio-video-is-off-sync