Home > Android > Play Video

Play Video

August 18th, 2009 Leave a comment Go to comments

Hello,

Here we are going to play a 3gp video in our Android application.

The important point we have to note is that video doesn’t plays when placed in the raw folder.Hence it has to be placed in the SDCARD itself.

So lets begin this interesting topic.

Ok, let us first insert our video:

  • You can get a short 3gp video from net (I got it from http://www.free-3gp-video.com).

  • In eclipse open perspective DDMS(Window –> Open Perspective –> DDMS) .

  • In DDMS perspective open view File Explorer(Window –> Show View –> File Explorer).

  • Also open Device view (Window –> Show View –> Device) and click the emulator you want to use(if emulator is not running then run a emulator with a sdcard option).

  • Then in File Explorer view navigate the tree shown and click sdcard node.

  • There are two buttons on the top right hand side of File Explorer as shown below

push a video onto the sdcard

push a video onto the sdcard

  • Click the button “Push a file onto the device” and in the file chooser navigate and choose the 3gp video.It should appear in the list of sdcard.(If not able to insert, check whether SDCARD is emulated in the emulator , in 1.5 sdk while creating a new Emulator there is a option of sdcard , insert 128M in the textbox next to it).

  • Ok, back to our code, open java perspective(Window –>Show View –> Java) and open layout file Main.xml.

  • Replace the its content with following contents:

<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<VideoView
android:id=”@+id/myvideo”
android:layout_gravity=”center”>
</VideoView>
</FrameLayout>

  • Now open your main activity and insert the following in the onCreate method as:

super.onCreate(savedInstanceState);

// set the view to our layout file

setContentView(R.layout.main);
// Video view: to view our video
VideoView video = (VideoView) findViewById(R.id.myvideo);

//set video path to our video(in this case man-cheetah-gazalle.3gp)
video.setVideoPath("/sdcard/man-cheetah-gazelle.3gp");
video.start();

  • Run the application and the video runs as shown below:

Video snapshot

Video snapshot

  • ClaveMartin

    I tried the samething but i couldn’t make it work. And i couldn’t set my emulator as sdcard node as well. I don’t know how to make sdcard node for emulator.

  • Jorgesys

    Hi ClaveMartin, use the command “mksdcard”

    usage::
    mksdcard -l “label” “size”"M for MB or K for KB” “file”
    example:: mksdcard -l sdcard 200M C:\Android\SDCARD\img200jorge.img

    :)

    p.d. this is the correct one (this post filters the characters “”:(

  • Zeba

    Hi,
    Can we have play , pause n stop buttons for the Video View ???
    N can dis be used for 3gp videos only???
    I wanted to play .wma and .mp3 videos…. how do I do dat??
    Thank you.

  • http://skyligh1.nycjava.net dario

    hi all,

    you’re not limited to the SD card, you can also play videos bundled with your app, just place it in the assets folder instead of the raw folder.

  • http://skylight1.nycjava.net dario

    hi all,

    you’re not limited to the SD card, you can also play videos bundled with your app, just place it in the assets folder instead of the raw folder.

  • jetti

    Hi to all,
    I executed this program and works fine but i want to play an video repeatedly infinite times and also until some condition matches…..
    Please help on this…..

  • Jui Bhatt

    Hello,

    Thanks a lot for this blog.
    But i think it is important to know that through this program video is played on platform 1.5 but not on 2.0 and 2.1.

  • Lalitendu

    Hi,

    I tried this. It’s working fine. It helped me a lot

    Thanks

  • sujit

    Hello,
    I tried this on 2.0 and it is working fine but in 1.5,2.1 it is not working.And also my video is not full screen though i have set the width and heigh of VideoView to “fill_parent” .

    Thanks.

  • Jason Brow

    @dario
    can you please let us know how we could do thia?

  • http://brindha@genisofttechnologies.com brindha

    Hi,

    I tried this application it can’t work.I run the application (The process com.example.android.video has stopped unexpectedly forceclose).what can i do? please help me.

  • http://brindha@genisofttechnologies.com brindha

    sorry guys.It’s work fine.

  • tarun jindal

    why not u publish whole code with project
    or send me on my email id
    i am new android develpoer and trying really hard to play a video through my application
    pleeeeeeeeeeeeeeeeeeeeease help

  • Nirali Gaudani

    getWindow().setFormat(PixelFormat.TRANSLUCENT);

    VideoView videoView = (VideoView)findViewById(R.id.viewvideo);
    videoView.setMediaController(new MediaController(this));

    MediaController mc = new MediaController(this);
    MediaPlayer mp = MediaPlayer.create(this, R.raw.wildlife);

    mp.start();

    I hv tried to play video using given above code.but i got error like “This Video Can’t Play”..so plz give me solution..

  • Nirali Gaudani

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    VideoView v = new VideoView(this);
    setContentView(v);
    v.setVideoPath(“android.resource://com.example.playvideo/”+R.raw.beach);
    v.start();

    }

    }

    try this code it is run perfect…

  • Ankitadavra

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    VideoView v=(VideoView)findViewById(R.id.video1);
    v.setVideoPath(“android.resource://com.example.PlayVideo/”+R.raw.cat);
    v.requestFocus();
    v.start();

    i write this code that run but picture can not display. plz tell me about this problem.

  • Sushrut Bidwai

    Thanks Nirali for helping out users of the site! If you are interested in writing for the blog, please contact me.

  • http://pulse.yahoo.com/_COULXSZ6VJ6ZELEFQGAHBGEONI chauhan

    This code not worked

  • Kaushik101in

    this will not working I have tried it
    for the last two days…

  • Suresh Sharma

    This is Okay demo of Vedio.challenge is now to play vedio file from sdcard.it sounds easy.But Obstacle is that File is >300 MB (like 1 GB). Android sdcard accessing doesn’t support >300 MB. So I decided to derive the Vedio in Smaller Chunks. My code is Followed.But Error is Vedio can’t be played.please have a look.see also DDMS LogCat.
    if Any Solution ?
    **CODE:**
       
       
        private void getBytesFromFile(File file) throws IOException {
            FileInputStream is = new FileInputStream(file); //videorecorder stores video to file

            java.nio.channels.FileChannel fc = is.getChannel();
            java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10485760);

            int chunkCount = 0;

            byte[] bytes;

            while(fc.read(bb) >= 0){
                bb.flip();
                //save the part of the file into a chunk
                bytes = bb.array();
                storeByteArrayToFile(bytes, “/sdcard/”+mRecordingFile + “.” + chunkCount);//mRecordingFile is the (String)path to file
                chunkCount++;
                bb.clear();
            }
        }

        private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {
            FileOutputStream fOut = new FileOutputStream(path);
            try {
                fOut.write(bytesToSave);
            }
            catch (Exception ex) {
                Log.e(“ERROR”, ex.getMessage());
            }
            finally {
                fOut.close();
            }
        }

    }

    **DDMS CatLog is**

    06-28 12:31:55.450: DEBUG/dalvikvm(2174): GC_EXPLICIT freed 170 objects / 11992 bytes in 126ms
    06-28 12:32:05.200: DEBUG/dalvikvm(2189): GC_EXPLICIT freed 700 objects / 38792 bytes in 126ms
    06-28 12:32:10.281: DEBUG/dalvikvm(2199): GC_EXPLICIT freed 44 objects / 2088 bytes in 162ms
    06-28 12:32:25.452: WARN/InputManagerService(79): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45107dd8
    06-28 12:35:34.713: DEBUG/SntpClient(79): request time failed: java.net.SocketException: Address family not supported by protocol
    06-28 12:40:34.779: DEBUG/SntpClient(79): request time failed: java.net.SocketException: Address family not supported by protocol
    06-28 12:45:34.803: DEBUG/SntpClient(79): request time failed: java.net.SocketException: Address family not supported by protocol

     

  • Kaushik101in

    I think you have to try for increasing the device size by right clicking on your
    project -> RunAs -> RunConfiguration -> Choose Target Tab

    in target tab you can see “Additonal Emulator Command Line Option” and write there a simple command line i.e. “-partition-size 1024″ and apply it. through this you can able to put a higher size of videos. And dont forget to put your video file into the res directory b’coz I have seen the error i.e. address family not supported. This kind of error happens when the project haven’t the compiled info about adding file or folder into the R.java file of the project.

    Tell me if it will work for you, I am also the begineer of Android and share some more experience and problems to grow along with……..

    Thanks
    Amit

  • Milind

    Thank YOu so mch for helping me HOW TO LOAD A FILE ON SD-CARD IN ANDROID

  • Ragummrsa

    Hi

    Can u tell how to find video orientation. like if the video was taken from landscape mode means it will play automatically landscape mode same as portrait mode.