使用 VideoView 从内部存储 android 播放视频文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10600167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:18:12  来源:igfitidea点击:

Play video file from internal storage android using VideoView

androidmedia-playerandroid-videoview

提问by baradas

Am trying to play a video stored in the internal storage in Android. However, no matter what I do it keeps coming back with either a -1 error or a -38 error. Both seem to be rather generic errors hence are not much intelligible.

我正在尝试播放存储在 Android 内部存储中的视频。但是,无论我做什么,它都会返回 -1 错误或 -38 错误。两者似乎都是相当普遍的错误,因此不太容易理解。

I am wondering if it is possible to use a VideoView and not a MediaPlayer instance to play a video file from the local storage.

我想知道是否可以使用 VideoView 而不是 MediaPlayer 实例来播放本地存储中的视频文件。

The steps involved in my app include,

我的应用程序中涉及的步骤包括,

  1. downloading a file from a remote url
  2. Storing the file in internal storage (note i use the convention for ensuring that it has global read permissions. i.e

    openFileOutput(file_name, Context.MODE_WORLD_READABLE);
    
  3. Reading the media file back at a later point from this location, and playing it in a videoView.

    String filePath = "file://" + getFilesDir()+File.separator+file_name;
    Uri videoUri = Uri.parse(filePath);
    Log.d("Video Player", filePath);
    videoPlayer.setVideoURI(videoUri);
    
  1. 从远程 url 下载文件
  2. 将文件存储在内部存储中(注意我使用约定来确保它具有全局读取权限。即

    openFileOutput(file_name, Context.MODE_WORLD_READABLE);
    
  3. 稍后从该位置读回媒体文件,并在 videoView 中播放。

    String filePath = "file://" + getFilesDir()+File.separator+file_name;
    Uri videoUri = Uri.parse(filePath);
    Log.d("Video Player", filePath);
    videoPlayer.setVideoURI(videoUri);
    

I also went through other links in StackOverflow which point out that I need to implement a CustomContentProvider to be able to read these files. Is there a direct way of accessing the file uri and setting it to the videoView without having to resorting to creating a custom content provider and using a mediaPlayer instead of a videoView.

我还浏览了 StackOverflow 中的其他链接,这些链接指出我需要实现一个 CustomContentProvider 才能读取这些文件。是否有一种直接访问文件 uri 并将其设置为 videoView 的方法,而不必求助于创建自定义内容提供程序并使用 mediaPlayer 而不是 videoView。

Other StackOverflow references used

使用的其他 StackOverflow 引用

  1. Android - Load video from private folder of app
  2. Can a videoview play a video stored on internal storage?
  1. Android - 从应用程序的私人文件夹加载视频
  2. videoview 可以播放存储在内部存储器上的视频吗?

回答by Joshua Pinter

Copy it to External Storage (Temporarily)

将其复制到外部存储(临时)

I ran into the same issue as you and resorted to just simply copying the file temporarily to external storage before playing it and then deleting the temporary file afterwards.

我遇到了和你一样的问题,在播放之前只是简单地将文件临时复制到外部存储,然后删除临时文件。

Here is some sample code that I used to do this:

这是我用来执行此操作的一些示例代码:

try {
  // Copy file to temporary file in order to view it.
  temporaryFile = generateTemporaryFile(file.getName());
  FileUtils.copyFile(file, temporaryFile);
  previewVideo(temporaryFile, videoView);

} catch (IOException e) {
  e.printStackTrace();
}


# Helpers

protected File generateTemporaryFile(String filename) throws IOException {
  String tempFileName = "20130318_010530_";

  File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

  File tempFile = File.createTempFile(
      tempFileName,       /* prefix     "20130318_010530" */
      filename,           /* filename   "video.3gp" */
      storageDir          /* directory  "/data/sdcard/..." */
  );

  return tempFile;
}

protected void previewVideo(File file, VideoView videoView) {
  videoView.setVideoPath(file.getAbsolutePath());

  MediaController mediaController = new MediaController(this);

  videoView.setMediaController(mediaController);

  mediaController.setMediaPlayer(videoView);

  videoView.setVisibility(View.VISIBLE);

  videoView.start();
}

回答by Basil

please try this. I have explained the procedure to play video from raw folder on this link: Video player not workig!. On that, if you modify

请试试这个。我已经在此链接上解释了从原始文件夹播放视频的程序:视频播放器不起作用!. 在这一点上,如果你修改

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);

with

Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"<path to your video>");

For example:

例如:

Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"/dcim/camera/2012-05-15_17-50-39_319.3gp");

I think will solve your issue. Remember to give the necessary permissions on the manifest.

我想会解决你的问题。请记住在清单上授予必要的权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

回答by ekawas

A little late to the game here, but if you add a FileProviderto your app, you can then create URIs that VideoView can utilize.

这里的游戏有点晚了,但是如果您将FileProvider添加到您的应用程序,您就可以创建 VideoView 可以使用的 URI。

It's actually pretty trivial to do and once you are done, you can access your files like:

这实际上非常简单,完成后,您可以访问您的文件,例如:

Uri video = FileProvider.getUriForFile(context, context.getPackageName(), new File(myAbsoluteVideoPath));

myVideoView.setVideoURI(video);

This can be used to play videos that you generated in your app and saved on the device.

这可用于播放您在应用中生成并保存在设备上的视频。