In the last post, I showed you how to integrate audios to your Android Application. Just like audios, we can also have our applications play videos if we want to.
However, one thing worth remembering is that once we are done with the programming part, we are going to want to run the application on an actual device not an emulator because it usually does not run the video file properly. Obviously, the video we are going to play will be stored on the sd card of the device and not in the application.
- Create an activity and set it’s content view. We will not be adding any buttons here. In here, I will show you how to run the video file just when the activity starts. You can, of course, trigger this on a button click.
- In the layout of the activity add a VideoView from the palette on the left. By default it occupies the complete space of the activity. This is exactly what we want. Again, you can limit the height and width according to your requirements.
- Switch over to the java file and write the following code just after the setContentView() method().
[java]
VideoView v = (VideoView)findViewbyId(R.id.videoView1);
v.setVideoPath("/sdcard/myvideo.mp4");
v.setMediaController(new MediaController(this));
v.start();
v.requestFocus();
[/java] - Save your work and execute it on an Android Device.
Understanding the Code
- VideoView is a default view to support videos.
- setVideoPath() : This function defines the exact location of the video file you want the application to run. Make sure you get the location, name and the format of the video file correct, failing which the application will behave in an unwanted manner.
- setMediaController() : This function displays the media controls like play/pause, fast forward, rewind. We may choose to have this or not. It is generally advisable to have one so that the user has some control over the playback.
- start() : It triggers the video playback.
- requestFocus() : It might so happen that there are other modules of our application being executed along with the video playback. This function causes the video playback to stay on top of all these modules.
COMPLETE SOURCE CODE
[java]
package com.nero.myfirstapp;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView v = (VideoView)findViewbyId(R.id.videoView1);
v.setVideoPath("/sdcard/myvideo.mp4");
v.setMediaController(new MediaController(this));
v.start();
v.requestFocus();
}
}
[/java]