Android Development Primer: Working with Videos in Android

This entry is part 19 of 50 in the series Android Development Primer

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().
    VideoView v = (VideoView)findViewbyId(R.id.videoView1);
    v.setVideoPath("/sdcard/myvideo.mp4");
    v.setMediaController(new MediaController(this));
    v.start();
    v.requestFocus();
    
  • 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

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();
    }
}

Series Navigation<< Android development primer: Working with Audio in AndroidAndroid Dev Primer: Adding Camera Functionality in Android >>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

42,383 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>