无法在Android中播放录制的视频

问题描述 投票:1回答:1

我想在Android中使用我自己的应用程序拍摄的mediaPlayer播放视频。

我这样设置mediaPlayer

private void setUpMediaRecorder() throws IOException {
    final Activity activity = getActivity();
    if (null == activity) {
        return;
    }
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
        mNextVideoAbsolutePath = getVideoFilePath();
    }
    mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
    mMediaRecorder.setVideoEncodingBitRate(10000000);
    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    switch (mSensorOrientation) {
        case SENSOR_ORIENTATION_DEFAULT_DEGREES:
            mMediaRecorder.setOrientationHint(ORIENTATIONS.get(rotation));
            break;
        case SENSOR_ORIENTATION_INVERSE_DEGREES:
            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
            break;
    }
    mMediaRecorder.prepare();
}

private void stopRecordingVideo() {
    mMediaRecorder.stop();
    mMediaRecorder.reset();
}

return Environment.getExternalStorageDirectory() + "/"
            + time + ".mp4"; // path, where the video should be stored

当我想播放拍摄的视频时,我收到以下错误:This video cannot be played。 你知道为什么那不起作用吗?我也尝试过Android-camera2video示例,并且代码也没有用。

android video mp4 android-mediarecorder android-camera2
1个回答
0
投票

从您的代码看起来您​​正在使用时间+“。mp4”作为名称。

基于错误和行为,“时间”很可能包含一些字符,这些字符要么混淆路径解析(例如/),要么以某种方式混淆文件扩展名,可能是通过添加和额外的'。'例如。

如果要将日期附加到文件名,则可以使用格式化程序将其设置为“安全”格式,例如:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
© www.soinside.com 2019 - 2024. All rights reserved.