Android 如何从 SD 卡播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10869830/
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 05:17:54  来源:igfitidea点击:

How to play videos from SD Card

androidandroid-videoview

提问by Mohit

I was creating a simple app which stream videos from net and I made it but now I want to change the code so that I can play video files from my SDCard

我正在创建一个简单的应用程序,它可以从网络上流式传输视频,但现在我想更改代码,以便可以从我的 SDCard 播放视频文件

original code:

原始代码:

Uri vidFile = Uri.parse("MY SITE HERE");
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
videoView.setVideoURI(vidFile);
videoView.setMediaController(new MediaController(this));
videoView.start();

So please help me with changing the code so that it can play videos from my mobile memory card.

所以请帮助我更改代码,以便它可以播放我的移动存储卡中的视频。

采纳答案by Anurag Ramdasan

The videoView.setVideoURI(vidFile);method needs to be replaced by the videoView.setVideoPath(path);method. Here path specifies the path of the video file on the SDCARD.

videoView.setVideoURI(vidFile);方法需要由被替换videoView.setVideoPath(path);方法。这里 path 指定了视频文件在SDCARD.

This path can be easily retrieved using the MediaStore.Video.Media.DATAproperty of that video file or by just entering the songpath statically as /sdcard/songname.

可以使用该MediaStore.Video.Media.DATA视频文件的属性轻松检索此路径,或者只需将歌曲路径静态输入为/sdcard/songname.

回答by Ronnie

Uri vidFile = Uri.parse(
   Environment.getExternalStorageDirectory().getAbsolutePath()+"filename");
... 

the rest of the code will be same.

其余代码将相同。

回答by Eight

In place of

代替

videoView.setVideoUri(vidFile)

videoView.setVideoUri(vidFile)

use

videoView.setVideoPath("/sdcard/SONG.").

videoView.setVideoPath("/sdcard/SONG.").

Let me know.

让我知道。

回答by user1653781

I also tried your code and got same error message but when I tried with video path with no blank space in path or name, it worked well. Just give it a try. e.g, file path "/mnt/sdcard/Movies/Long Drive Song - Khiladi 786 ft. Akshay Kumar_Asin-YouTube.mp4" gave the error but file path "/mnt/sdcard/Movies/Khiladi.mp4" worked well.

我也尝试了您的代码并收到了相同的错误消息,但是当我尝试使用路径或名称中没有空格的视频路径时,它运行良好。试试吧。例如,文件路径“/mnt/sdcard/Movies/Long Drive Song - Khiladi 786 ft. Akshay Kumar_Asin-YouTube.mp4”给出了错误,但文件路径“/mnt/sdcard/Movies/Khiladi.mp4”运行良好。