Android SDK - 来自 URL 的媒体播放器视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11411792/
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
Android SDK - Media Player Video from URL
提问by user1417302
I've tried to find a simple tutorial which explains how to load a video from a URL into the android media player but unfortunately I couldn't find any!
我试图找到一个简单的教程,它解释了如何将视频从 URL 加载到 android 媒体播放器中,但不幸的是我找不到任何教程!
I have tried several things to try get it working but still no luck.
我已经尝试了几件事来尝试让它工作,但仍然没有运气。
What is the best way to have a MediaPlayerActivity just load a video from a URL?
让 MediaPlayerActivity 仅从 URL 加载视频的最佳方法是什么?
Thanks
谢谢
EDIT:
编辑:
I have tried the following code as suggested:
我已经按照建议尝试了以下代码:
VideoView videoView = (VideoView) findViewById(R.id.your_video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse("url-here"));
videoView.start();
It just crashes when I go to this activity.
当我去参加这个活动时它就会崩溃。
回答by Angelo
You can use a VideoView. Here is an example:
您可以使用VideoView。下面是一个例子:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
//Use a media controller so that you can scroll the video contents
//and also to pause, start the video.
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(videoUrl));
videoView.start();
EDIT
编辑
You should provide the URI (having a String url
variable, where it has the url of the video) with this code Uri.parse(url)
. And also be sure if the url is appropriate. Also, have you provided the appropriate permissions to your app, as AkashG suggested, (since it uses the internet you will need to add <uses-permission android:name="android.permission.INTERNET" >
in your app's Manifest.xml
)? Finally you should define your activity MediaPlayerActivity
in in your app's Manifest.xml
您应该提供String url
带有此代码的 URI(具有一个变量,其中包含视频的 url)Uri.parse(url)
。还要确定 url 是否合适。此外,您是否如 AkashG 建议的那样为您的应用程序提供了适当的权限(因为它使用互联网,您需要添加<uses-permission android:name="android.permission.INTERNET" >
到您的应用程序的Manifest.xml
)?最后,您应该MediaPlayerActivity
在应用程序的Manifest.xml
End of EDIT
编辑结束
You can also use MediaPlayer. The Android developers site has a good tutorial here.
您也可以使用MediaPlayer。Android 开发者网站在这里有一个很好的教程。
回答by AkashG
you can load video from url as:
您可以从网址加载视频:
VideoView videoView = (VideoView) findViewById(R.id.view);
MediaController controller = new MediaController(this);
videoView.setVideoPath("url of the video");
videoView.setMediaController(controller);
videoView.start();
Add internet permissionin manifest file.
在清单文件中添加互联网权限。
回答by MobileEvangelist
I think you can find useful thinks Here..about playing video from different sources.
我认为您可以在这里找到有用的想法……关于播放来自不同来源的视频。
If your using Emulator , First, do not use the emulator for testing video playback. Its ability to handle video playback is very limited. Use an actual Android device.
如果您使用 Emulator ,首先不要使用模拟器来测试视频播放。它处理视频播放的能力非常有限。使用实际的 Android 设备。
Second, always check LogCat (adb logcat, DDMS, or DDMS perspective in Eclipse) for warnings when you run into multimedia problems. OpenCORE -- the multimedia engine used by Android -- has a tendency to log error-level conditions as warnings.
其次,当您遇到多媒体问题时,始终检查 LogCat(Eclipse 中的 adb logcat、DDMS 或 DDMS 透视图)是否有警告。OpenCORE——Android 使用的多媒体引擎——倾向于将错误级别的情况记录为警告。
For example, your video file may not be set up for progressive download, which is required for HTTP streaming. On Linux, you can patch up MP4 videos for progressive download by installing MP4Box and running MP4Box -hint .
例如,您的视频文件可能没有设置为渐进式下载,这是 HTTP 流媒体所必需的。在 Linux 上,您可以通过安装 MP4Box 并运行 MP4Box -hint 来修补 MP4 视频以进行渐进式下载。
Hope this explanation works for you..
希望这个解释对你有用..