Android Camera2 Video Playback Video and Audio Out of Sync

不羁岁月 提交于 2019-12-23 02:36:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!