Tutorial How to Create Simple Android Code to Reproduce Videos in your Google Android Application

Tutorial How to Create Simple Android Code to Reproduce Videos in your Google Android Application
Page content

Introduction

The purpose of this article is to show with a little code how to reproduce MP4 format videos from your SDcard. The code has been reduced to the most simple case, just for better understanding.

In this example we will need to create an Activity in our project (I have called it “VideoPlayer.java”) and create a specific element in our XML layout (the SurfaceView) bound to this Activity. Here we go!

Layout Code and Android Manifest

To reproduce our video, we will need a specific user interface element to do it. This element will be a “SurfaceView” and will be placed in our layout XML file. Here is the piece of code that is needed:

<RelativeLayout xmlns:android=“https://schemas.android.com/apk/res/android"

android:layout_width=“fill_parent” android:layout_height=“fill_parent”>

<SurfaceView android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:id=”@+id/screen_tutorial_video_surface"></SurfaceView>

</RelativeLayout>

I have placed this code in layout_video.xml file inside the /res folder in our Android architecture.

We have a RelativeLayout and the Surfaceview that will fill the whole screen. The video will be played in the Surfaceview element. As I said before, this is the most simple example, but you can do it in a more complex way by adding more elements to your UI, buttons, labels… just don’t forget where your video is going to be reproduced.

The Surfaceview element is a dedicated space in the views hierarchy to draw. It is used to draw OpenGL elements (explained here in the Introduction to OpenGL in Android) or as we are doing in our example, reproducing a video file.

About the AndroidManifest.xml, we need to add the new Activity to it.

<activity android:name=".video.VideoPlayer"></activity>

And that’s all for the XML code, let’s check the Android code

Android code - part 1

As I said in the introduction, I have created an Activity called “VideoPlayer.java”. This is the class header:

public class VideoPlayer extends Activity implements SurfaceHolder.Callback{

You can see that the VideoPlayer class extends from Activity and implements the SurfaceHolder.Callback interface. This interface helps us to get information from the state of the Surfaceview we have created in our XML file. This way we will know when the surface is ready to be used, for example.

Because of the implementation of the SurfaceHolder.CallBack in our class, it’s mandatory to add the following methods to our code:

@Override

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

// TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {

}

@Override

public void surfaceDestroyed(SurfaceHolder arg0) {

// TODO Auto-generated method stub

}

For the minimum functional code, we are just going to use the surfaceCreated method. This method is called when the surface is created and ready to be drawn!

Now let’s introduce the variables we are going to use in our code:

private MediaPlayer mMediaPlayer;

private SurfaceView mPreview;

private SurfaceHolder holder;

private String videoPath = “/sdcard/my_video.mp4”;

MediaPlayer class is used to play video and audio in stream.

Surfaceview class, we get the Surfaceview from the XML code to use it in our Android code.

SurfaceHolder class helps us control the surface (size, format of the surface… if the surface changes).

And the object videoPath is just the path where our video is stored. I have placed it in the root of the SDcard.

Android code - part 2

The following piece of code is the onCreate:

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.layout_video);

iniElements();

}

Here we set the layout view (layout_video.xml) to the Activity with the setContentView method, and we call the iniElements() function. When I write code, I like to have everything well separated, this is an example, just a function to initialize the elements I’m going to use in the application. Here is the function:

private void iniElements() {

mPreview = (SurfaceView) findViewById(R.id.screen_tutorial_video_surface);

holder = mPreview.getHolder();

holder.addCallback(this);

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

holder.setFixedSize(getWindow().getWindowManager().getDefaultDisplay().getWidth(), getWindow().getWindowManager().getDefaultDisplay().getHeight());

mMediaPlayer = new MediaPlayer();

mMediaPlayer.setDisplay(holder);

}

Here we are doing a bunch of things.

We are getting the Surfaceview from the XML to use it in our Android code with the findViewById method. Once we have the SurfaceView in our Android code, we get the holder (mPreview.getHolder() ) and store it in a holder object. Now we can define some parameters of the surface like its size (setFixedSixe), the surfaceType (setType) and the callback implementation that we are going to use (addCallback). We create the mediaPlayer object and we add the surfaceView holder to it (setDisplay).

Hint! - With the getWindow().getWindowManager().getDefaultDisplay().getWidth() and the getWindow().getWindowManager().getDefaultDisplay().getHeight() methods we can get the screen size of our Android display.

Now I introduce the code that plays the video:

private void iniPlayer() {

try {

mMediaPlayer.setDataSource(videoPath);

mMediaPlayer.prepare();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mMediaPlayer.start();

}

In this method, we set the video path to the mediaPlayer object, we prepare the video to be played, we set the audio type (setAudioStreamType) and we start it!!!! I have placed some try-catches to get the possible exceptions using the setDataSource and prepare methods.

The iniPlayer() method is going to be called from the surfaceCreated method. This way we can be sure to play the video once the Surface is ready!

public void surfaceCreated(SurfaceHolder holder) {

iniPlayer();

}

And with this, we have a simple way to reproduce videos in our Android application.

Tests cases

This code has been tested using the HTC myTouch (HTC Magic in Europe), HTC Dream and with Android 1.6 version, my testing devices.

You can find the full source code in the next link.

Bright Hub Example source code