Working with MediaController, VideoView in Android

Posted By : Ravi Sharma | 11-Feb-2013

This blog will be helpful to those who would like to use MediaController, include Video Content in their android apps.

Playing video content in android is fairly simple. Here, I will be talking about how to play video from Amazon s3.

In your xml file , paste the following-

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <VideoView
       android:id="@+id/video_view"
       android:layout_width="match_parent"
       android:layout_height="600dp"
      />

</LinearLayout>  

 

Here, VideoView is the view which will allow you to play the video at runtime.you may set width, height accordingly. In your activity, use the following code to play video-

 

void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play_video);
		video = (VideoView) findViewById(R.id.video_view);

		new BackgroundAsyncTask()
				.execute("https://s3.amazonaws.com/TranscodeAppVideos/ocean.mp4");

						
			
	}

I have used AsyncTask to perform background operations.

The parameter passed is the url of the video stored in Amazon s3 bucket.

Following is the Background task.


public class BackgroundAsyncTask extends AsyncTask<String, Uri, Void> {
		Integer track = 0;
		ProgressDialog dialog;

		protected void onPreExecute() {
			dialog = new ProgressDialog(PlayVideo.this);
	        dialog.setMessage("Loading, Please Wait...");
	        dialog.setCancelable(true);
	        dialog.show();
		}

		protected void onProgressUpdate(final Uri... uri) {

			try {

				media=new MediaController(PlayVideo.this);
				video.setMediaController(media);
				media.setPrevNextListeners(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// next button clicked

					}
				}, new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						finish();
					}
				});
				media.show(10000);

				video.setVideoURI(uri[0]);
				video.requestFocus();
				video.setOnPreparedListener(new OnPreparedListener() {

				    public void onPrepared(MediaPlayer arg0) {
				    	video.start();
						dialog.dismiss();
				    }
				});
				

			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		}

		@Override
		protected Void doInBackground(String... params) {
			try {
				Uri uri = Uri.parse(params[0]);
				
				publishProgress(uri);
			} catch (Exception e) {
				e.printStackTrace();

			}

			return null;
		}

		
	}

The string passed is parsed as Uri. Then onProgressUpdate is called where ultimately video is played.

Playing online video is a long task so I have used Progress dialog which indicates of long background progress going on.

Also, I have used MediaController class so that you could see the pause, back and next button on tapping the video.

You can also program the functionality on back , next button like I have done here.

 

media.setPrevNextListeners(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// next button clicked

					}
				}, new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						finish();
					}
				});
				media.show(10000);

I finish the current activity playing video on click of back button.

Also use the internet permission in the Android Manifest file.

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

And , finally after running the app you should see something like below-

Thanks,

 

Ravi Sharma,

[email protected]

 

About Author

Author Image
Ravi Sharma

Ravi Sharma is an Android application developer with experience in Java , Titanium and Phonegap frameworks. Ravi loves drawing and PC games.

Request for Proposal

Name is required

Comment is required

Sending message..