How to play a video in Android
Here are code snippets that let your application play video files. The first code plays a video from URL or SD card.
Android play video from URL or SD card
VideoView video = (VideoView)findViewById(R.id.videoView); // URL or local path here video.setVideoPath("http://download.itcuties.com/teaser/itcuties-teaser-480.mp4"); video.start();
Check out supported video formats at http://developer.android.com/guide/appendix/media-formats.html.
Next snippet lets you play a video that is hosted on the YouTube platform. This code starts a browser or a built in YouTube application that runs the video.
Android play video from YouTube
Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("http://www.youtube.com/watch?v=DdlWPL53PvQ")); SelectActivity.this.startActivity(i);
Example
Here is an example usage of the above codes in an application. This application displays two buttons. When you push ‘play from URL’ button application plays the video available at a given URL. When you push ‘play on youtube’ button application start’s a browser navigates to a given YouTube Video URL.
Here is an activity code that runs ‘under’ application select screen.
SelectActivity
package com.itcuties.android.videoplayer; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * Select video source activity. * * @see ITCuties */ public class SelectActivity extends Activity implements OnClickListener { private Button playFromUrlButton; private Button playOnYouTubeButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select); playFromUrlButton = (Button) findViewById(R.id.buttonURL); playOnYouTubeButton = (Button) findViewById(R.id.buttonOnYouTube); // Set onClickListener playFromUrlButton.setOnClickListener(this); playOnYouTubeButton.setOnClickListener(this); } @Override public void onClick(View v) { // Check which button was clicked if (playFromUrlButton.isPressed()) { Intent i = new Intent(SelectActivity.this, URLVideoActivity.class); SelectActivity.this.startActivity(i); } else if (playOnYouTubeButton.isPressed()) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("http://www.youtube.com/watch?v=DdlWPL53PvQ")); SelectActivity.this.startActivity(i); } } }
Here is an activity code that plays a video from URL.
URLVideoActivity
package com.itcuties.android.videoplayer; import android.app.Activity; import android.os.Bundle; import android.widget.VideoView; /** * Play Video from URL * * @see ITCuties */ public class URLVideoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.url_activity_video); VideoView video = (VideoView)findViewById(R.id.videoView); video.setVideoPath("http://download.itcuties.com/teaser/itcuties-teaser-480.mp4"); video.start(); } }
Download this sample code here.
This code is available on our GitHub repository as well.
When i use video view control with programming .it will run fine but my issue is following.
1.if video reach the specific duration after i change screen orientation.i.e portrait to landscape .
2.that time it will loading fro the start video.
mean when i use video to pause after resume i cant get this video started at current position.please help me .
thank you.
Hi, this behavior is due the fact that whenever you rotate the device a new activity is started – this is the default behavior of Android. You should have some kind of data layer in your application which is called from the activity, so that activities’ code doesn’t load data from beginning like in our example.
If you are looking for an advanced tutorial on how to play videos from YouTube see this one – http://www.itcuties.com/android/play-youtube-video/.
Thanks a lot. Nice tutorial.
displaying a dialod with error msg is “Can’t Play video”