Wednesday, January 19, 2011

Android Learning Series Part II - Sample1

Android Sample 1 – Play Local Video file

Let us create a sample project to play a video file in Android. This sample is based on simulators and eclipse IDE and not testing real device. 

Pre-requisite

What you will learn

Create an android project and run a local video file using the VideoView interface. The local video file is kept as an android resource and the sample demonstrates how to access the resources without directly referring file system.

Create Project

Follow below steps to create the project from eclipse IDE.
1. Select “File -> New -> Android Project”
2. Type a Project Name.
3. Select the Target Name as “Android 2.2”. [You can choose the version of your choice]
4. Type the application name
5. Type the package name
6. Check the box “Create Activity”
7. Type the name of the Activity as “VideoStreamActivity”
8. Click Finish to create the project.
9. The project now will be created in the eclipse IDE

Coding for Video Streaming

1. Explore your project
2. Open the xml file “res/layout/main.xml”
3. If you are seeing the xml, then shift to the “GraphicalLayout”.
4. From the left hand view options, location “VideoView” and drag it to below the “Hello World” TextView
5. Save the layout.
6. Copy your video files to the location “res/raw” folder. If raw folder doesn’t exist then create one. In my case it was “sample.3gp”
7. Open the “src/<>/VideoStreamActivity.java” file
8. Define two global variable as below
private VideoView videoView;
private TextView textView;

9. replace the onCreate method stub with following code.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
videoView = (VideoView) findViewById(R.id.VideoView01);
textView = (TextView) findViewById(R.id.TextView01);
System.out.println("Init Video view");
streamVideo();

10. create streamVidoe method as below and change the reference “R.raw.sample” to the video file you have copied to res/raw folder in 6 above.
private void streamVideo() {
try {
Uri ur = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sample);
textView.setText("Playing .."+ ur.toString());
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoURI(ur);
videoView.start();
}catch(Exception e) {
textView.setText(e.getLocalizedMessage());
}
}

11. resolve the imports as
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

12. Save the file

Run the Sample

Right click your project and run the Sample as “Android Application”. You can see the emulator starts up and playing the video.

Errors & Resolution

The below section lists the errors and resolutions I have gone through with this sample.

1. While running, I got the error “Sorry, This video cannot be played”.
Answer: Understanding this error took lot of time. I googled a lot but could not found resolutions. Some of the articles talking about the error is result of missing media players and hardware configuration of the PC where emulator is running. But finally I found the issue was the way android resources are referenced using code. You have to use the protocol “android.resource:// [package name]/ [relative path].

e.g. “android.resource://com.sajith.test/res/raw/sample.3gp”.

Use getPackageName() method to dynamically resolve the path name during runtime.

njoy
Sajith