如何从外部存储播放视频文件

时间:2017-02-07 13:11:06

标签: android

录制视频后

   mMediaRecorder
      .setOutPutFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

创建媒体文件

         private static File getOutputMediaFile(int type) {
         File mediaStorageDir = newFile(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "Pitch");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("Pitch", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

我发送视频路径的目的是为了显示视频

         public void RecieveUri() {
          Intent intent = new Intent(this,VideoPlayBackActivity.class);
          vidPath =  getOutputMediaFile(MEDIA_TYPE_VIDEO);
          vidP = vidPath.getAbsolutePath();
          intent.putExtra(EXTRA_MESSAGE,vidP);
          startActivity(intent);
          }

如果文件路径不为空,我收到了检查的文件路径

          Intent intent = getIntent();
          vidPATH = intent.getStringExtra(CameraActivity.EXTRA_MESSAGE);
          if(vidPATH.isEmpty()){
          Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
          }
          else{
          Toast.makeText(this,"Path: "+vidPATH,Toast.LENGTH_SHORT).show();
          }

然后我尝试使用媒体播放器播放视频

        try{
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(vidPATH);

        mMediaPlayer.setSurface(surface);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepareAsync();

但它没有播放屏幕保持黑色,日志猫说             W / System.err:java.io.IOException:setDataSource failed。

0 个答案:

没有答案