Android SDK - Media Player Video from URL

user1417302 picture user1417302 · Jul 10, 2012 · Viewed 41.9k times · Source

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!

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?

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.

Answer

Angelo picture Angelo · Jul 10, 2012

You can use a VideoView. Here is an example:

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

End of EDIT

You can also use MediaPlayer. The Android developers site has a good tutorial here.