Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取'MediaPlayer﹕;错误(1,-2147483648)`在从sd卡创建后立即尝试播放视频文件时_Android_Video_Android Camera_Android Mediaplayer_Video Recording - Fatal编程技术网

Android 获取'MediaPlayer﹕;错误(1,-2147483648)`在从sd卡创建后立即尝试播放视频文件时

Android 获取'MediaPlayer﹕;错误(1,-2147483648)`在从sd卡创建后立即尝试播放视频文件时,android,video,android-camera,android-mediaplayer,video-recording,Android,Video,Android Camera,Android Mediaplayer,Video Recording,我已经从同样的问题中读到了很多答案,比如 我现在正在制作带有录制视频功能的定制相机 我可以拍摄照片,然后正确查看照片 但录制视频时出现问题,然后立即查看视频 我想在录制视频后立即通过参数文件路径播放视频,而不是下次打开应用程序 我已经试过了: 1-扫描安卓系统已识别的新文件的媒体 2-试试放固定的文件路径,就行了,可以播放了“/mnt/sdcard/Pictures/Enterprise/VID_20150327_143003.mp4”。但这不是我想要的 知道如何在录制视频后立即通过参数文件路径

我已经从同样的问题中读到了很多答案,比如

我现在正在制作带有录制视频功能的
定制相机

我可以
拍摄照片,然后正确查看照片

但录制视频时出现
问题,然后立即查看视频

我想在录制视频后立即通过参数
文件路径
播放视频,而不是下次打开应用程序

我已经试过了:

1-扫描安卓系统已识别的新文件的媒体

2-试试
放固定的文件路径,就行了
,可以播放了<代码>“/mnt/sdcard/Pictures/Enterprise/VID_20150327_143003.mp4”
。但这不是我想要的

知道如何在录制视频后立即通过参数
文件路径
播放视频的人

请告诉我

谢谢

p/s:

  • 我的案例是在成功录制视频后直接从sd卡播放视频,而不是从服务器流式播放视频

  • 安卓4.1-索尼Xperia U

  • 只有我的应用程序无法在成功录制视频后立即播放视频。如果使用默认媒体播放器应用程序(另一个应用程序)播放视频正常

  • 另一件事,请注意,如果使用固定的文件路径可以播放现有的视频

错误

MediaPlayer﹕ 错误(1,-2147483648)
MediaPlayer﹕ 错误(1,-2147483648)

LOGCAT

I/ExternalStorage﹕ Scanned             /mnt/sdcard/Pictures/Enterprise/VID_20150327_153645.mp4:
I/ExternalStorage﹕ -> uri=content://media/external/video/media/21992
I/﹕ FILE_PATH /mnt/sdcard/Pictures/Enterprise/VID_20150327_153645.mp4
E/MediaPlayer﹕ error (1, -2147483648)
E/MediaPlayer﹕ Error (1,-2147483648)
编辑我得到了答案。多谢各位

在代码下方播放视频

private void showVideoOnUI() {
    Log.i("", "FILE_PATH " + FILE_PATH);

    // THIS LINE SHOW THE VIDEO TOTALLY CAN BE PLAYED
    //        mVvVideo.setVideoPath("/mnt/sdcard/Pictures/Enterprise/VID_20150327_143003.mp4");

    // ------- ERROR HAPPEN IN THIS LINE ---------
    // FILE PATH = /mnt/sdcard/Pictures/Enterprise/VID_20150327_143555.mp4 also,
    // BUT THE VIDEO TOTALLY CAN NOT BE PLAYED
    mVvVideo.setVideoPath(FILE_PATH);
    //        mVvVideo.setVideoURI(Uri.parse(FILE_PATH));

    // set play video view dialog details photo
    MediaController mMc = new MediaController(getActivity());
    mMc.setAnchorView(mVvVideo);
    mMc.setMediaPlayer(mVvVideo);

    mVvVideo.requestFocus();
    mVvVideo.setBackgroundColor(Color.WHITE);
    mVvVideo.setMediaController(mMc);
    mVvVideo.setZOrderOnTop(true);
    mVvVideo.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mVvVideo.isPlaying()) {
                mVvVideo.pause();

                // Show full-screen button again
                mVvVideo.setVisibility(View.VISIBLE);
            } else {
                mVvVideo.start();
            }

            return false;
        }
    });

    if (!mVvVideo.isPlaying())
        mVvVideo.start();
}
private boolean prepareVideoRecorder(int mode){
    // Should release before use new Preview for Recording Video mode
    CustomCamera.releaseCamera();

    // Initialize camera
    CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);

    // Set orientation display
    CustomCamera.setCameraDisplayOrientation(getActivity(), mode);

    // Should release before use new Preview for Recording Video mode
    CustomCamera.releaseMediaRecorder();

    CustomCamera.mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    CustomCamera.mCamera.unlock();
    CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);

    // Step 2: Set sources
    CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    //        CustomCamera.mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    //        CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    //        CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

    // Step 4: Set output file
    CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());

    // Step 5: Set the preview output
    CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        CustomCamera.mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        CustomCamera.releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        CustomCamera.releaseMediaRecorder();
        return false;
    }
    return true;
}
下面是代码扫描介质,以查看创建后的新文件

/**
 * Create a File for saving an image or video
 */
private File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), define.Camera.ENTERPRISE);

    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.i("", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = null;
    if (type == MediaType.PHOTO) {
        mediaFile = new File(
                mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + Extension.JPG);
    } else if (type == MediaType.VIDEO) {
        mediaFile = new File(
                mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + Extension.MP4);
    }

    String FILE_PATH = mediaFile.getAbsolutePath();
    // todo Should refresh Sd card in here
    File mFile = new File(FILE_PATH);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mFile.getAbsolutePath(), mFile.getName());
        Uri contentUri = Uri.fromFile(f);

        Log.i("", "contentUri - " + contentUri.toString());

        mediaScanIntent.setData(contentUri);
        getActivity().sendBroadcast(mediaScanIntent);
    } else {
        getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + mFile.getParent())));
    }

    // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(getActivity(),
            new String[]{FILE_PATH}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    // ExternalStorage﹕ Scanned /mnt/sdcard/Pictures/Enterprise/VID_20150327_151212.mp4:
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    // ExternalStorage﹕ -> uri=content://media/external/video/media/21973
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
            });
    return mediaFile;
}
这里是MANIFEST.XML文件

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name=".Enterprise"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="ui.activity.CustomCamera"
        android:screenOrientation="portrait"
        android:exported="true">

        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file"/>
        </intent-filter>

    </activity>
    <activity android:name="ui.activity.CustomGallery" />

</application>
这里的代码设置录制视频

private void showVideoOnUI() {
    Log.i("", "FILE_PATH " + FILE_PATH);

    // THIS LINE SHOW THE VIDEO TOTALLY CAN BE PLAYED
    //        mVvVideo.setVideoPath("/mnt/sdcard/Pictures/Enterprise/VID_20150327_143003.mp4");

    // ------- ERROR HAPPEN IN THIS LINE ---------
    // FILE PATH = /mnt/sdcard/Pictures/Enterprise/VID_20150327_143555.mp4 also,
    // BUT THE VIDEO TOTALLY CAN NOT BE PLAYED
    mVvVideo.setVideoPath(FILE_PATH);
    //        mVvVideo.setVideoURI(Uri.parse(FILE_PATH));

    // set play video view dialog details photo
    MediaController mMc = new MediaController(getActivity());
    mMc.setAnchorView(mVvVideo);
    mMc.setMediaPlayer(mVvVideo);

    mVvVideo.requestFocus();
    mVvVideo.setBackgroundColor(Color.WHITE);
    mVvVideo.setMediaController(mMc);
    mVvVideo.setZOrderOnTop(true);
    mVvVideo.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mVvVideo.isPlaying()) {
                mVvVideo.pause();

                // Show full-screen button again
                mVvVideo.setVisibility(View.VISIBLE);
            } else {
                mVvVideo.start();
            }

            return false;
        }
    });

    if (!mVvVideo.isPlaying())
        mVvVideo.start();
}
private boolean prepareVideoRecorder(int mode){
    // Should release before use new Preview for Recording Video mode
    CustomCamera.releaseCamera();

    // Initialize camera
    CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);

    // Set orientation display
    CustomCamera.setCameraDisplayOrientation(getActivity(), mode);

    // Should release before use new Preview for Recording Video mode
    CustomCamera.releaseMediaRecorder();

    CustomCamera.mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    CustomCamera.mCamera.unlock();
    CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);

    // Step 2: Set sources
    CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    //        CustomCamera.mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    //        CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    //        CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

    // Step 4: Set output file
    CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());

    // Step 5: Set the preview output
    CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        CustomCamera.mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        CustomCamera.releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        CustomCamera.releaseMediaRecorder();
        return false;
    }
    return true;
}
我终于认识到自己的错误。对不起。我需要编辑代码如下

// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    // Step 4: Set output file
    RECORDED_FILE_PATH = getOutputMediaFile(MediaType.VIDEO).toString();
    CustomCamera.mMediaRecorder.setOutputFile(RECORDED_FILE_PATH);


好了,我现在有办法了

这是我的错。我调用了两次
getOutputMediaFile()
方法,因此它创建了两个文件:

  • 1文件通过使用流传输数据。[此文件未读取我的应用程序]

  • 1文件没有数据[此文件我的应用程序已读取]

因此,
video不能播放
对话框出现,我也得到了这个错误
MediaPlayer﹕ 错误(1,-2147483648)

p/s:我希望我能解释一下为什么会出现这种
UNKNOWN\u错误

很抱歉


谢谢。

可能是@maven的副本我已经看过答案了。这看起来像是建议,建议表明,相关的东西作为编码器。。。实际上这不是解决办法,不是吗?你怎么认为?或者你明白答案表明什么?请告诉我。谢谢。没有直接的解决方案,您必须验证录制的视频格式并制作它。
“录制的视频格式并使其与编码器兼容”
-实际上默认的媒体播放器可以播放我录制的视频。那么这还不够?正如我在问题中所说,我的应用程序只有在成功录制视频后才能立即播放。如果我通过了
固定文件路径
值,它完全可以播放。错了吗?