How to play a Video file

8

This is a sample activity which shows How to run a video file.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project VideoViewExample.
2.) Put a video file in raw folder and keep it inside the res folder.
3.) Put the following code snippet in main.xml:

<?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/surface_view" 
       android:layout_width="320px"
       android:layout_height="240px"/>
</LinearLayout>

4.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. VideoViewExample. Enter following information:
Project name: VideoViewExample
Build Target: Google APIs
Application name: VideoViewExample
Package name: com.sample.VideoViewExample
Create Activity: VideoViewExample

On Clicking Finish VideoViewExample code structure is generated with the necessary Android Packages being imported along with VideoViewExample.java. VideoViewExample class will look like following:

package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewExample extends Activity {
   private VideoView mVideoView;

   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);
     mVideoView = (VideoView) findViewById(R.id.surface_view);
     mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
     mVideoView.setMediaController(new MediaController(this));
     mVideoView.requestFocus();
   }
}

Output –The final output:

Did you enjoy this article?
Share
the
Love
Get Free Updates
  • Dharmesh Garg

    what are the supported file formats which can be played on android emulator

  • http://edumobile.org Sushant

    It supports H.263 encoding and decoding, H.264 AVC and MPEG-4 SP both only decoding.

    Checkout the chart of all supported media formats for more information.

  • Dewa

    thanks for the tutorial. i try in emulator but the only thing that shown just the voice, but not the video. Can you point me the error?

  • Invictus

    no the video is shown alright..but eclipse emulator really sucks. if you really want a HD experience, try using an actual device. however, use the standard virtual device you always use. using others might not always work. anyways, a very ice piece of work. i am new to android development..and videoview really made it simple. trying to design a player from scratch can be quite taxing. pheeww!

  • Girish

    Hi, I want to know , can we play video in a browser or can we make video conferencing in android ?

  • preeti jain

    can i play the mp4 format video in the emulator through raw folder

  • preeti jain

    sir when i try to play the video from the raw folder.the logcat show the following error:
    unable to create media player
    could not open file on client side ,trying server side.

  • juhi

    what is the meaning of getpakagename()

Proudly designed by Theme Junkie.