Thursday, December 16, 2010

Play Video From raw Folder In Android

Hello ,

This is the simple application for play video in android. This video play from raw folder.

And this are the simple of basic application .

And this are the source code for play video from raw folder.

First save video in res/raw folder. And then complete you code given below.

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="fill_parent"
android:layout_height="fill_parent"
>
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/VideoView"></VideoView>
</LinearLayout>

Video.java

package com.image;

import java.net.URI;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class image extends Activity {
private static final String TAG = "PRANJAL";
private boolean isImage = false;
private String reviewImageLink;
private MediaController mc;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vd = (VideoView) findViewById(R.id.VideoView);

Uri uri = Uri.parse("android.resource://" + getPackageName() + "Name OF Video store in raw folder");

mc = new MediaController(this);
vd.setMediaController(mc);

vd.setVideoURI(uri);
vd.start();
}

}

I think u have useful this code.

41 comments:

  1. good tutorial use
    Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);

    ReplyDelete
  2. I've tried every concievable method of playing a video. Used VideoView, MediaPlayer, MediaController, none of them seem to work seperately or in unison to play a raw file from a folder. This example also is not working for me. Still getting the typical "Sorry this video cannot be played message" I did at one point get a streaming video to not display this message, but it didn't play the video, only displayed a still image and audio.

    ReplyDelete
  3. just make sure to make the change pankaj said & it works perfectly!! :)
    though, you dont need any of those private variables except for mediacontroller

    so the steps are:
    1) make sure you have a VideoView in your xml file (can leave the ID as VideoView)
    2) in your java code, add a private global variable: private MediaController mc;
    3) in the java onCreate method, add the following code:

    VideoView vd = (VideoView) findViewById(R.id.VideoView);

    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video_name_without_file_extension);

    mc = new MediaController(this);
    vd.setMediaController(mc);

    vd.setVideoURI(uri);
    vd.start();

    ReplyDelete
  4. thanks a ton !!.. this really helped. esp the URI part ..
    Thanks again
    surya

    ReplyDelete
  5. hi all!!!
    Pls tell me wen i create a a folder called res/raw its getting saved in layout by default. is it correct. and i cannot open the raw folder to add video. what type of videos can i play(mp4,flv...).

    secondly i need a code to record a video and save it. pls help me in developing this application in android. I shall be very much thankful to u!!!
    pls reply me asap since i need to finish this work by monday!! Sorry if i am pressuring you.

    Thanks in advance.

    Regards,
    Raghav Rajagopalan

    ReplyDelete
  6. hey!!! Thanks it worked perfectly for me!!! Any way thanks for the tutorial!! All the best!!! Thanks once again..

    ReplyDelete
  7. Hi, I'm still facing a problem and it is that the audio is working fine but not the video. Its an mp4 file.

    ReplyDelete
  8. Hello someone,

    Please help me how to play large video in android.

    i want to play 1Gb video from sdcard facing outof memory exception.Please help me, My work is delay due to this.

    ReplyDelete
  9. hello Naveen Kumar,

    Please upload this video on your server. And this URl put on your application.

    Its easy way for used any video.

    ReplyDelete
  10. Dear sir,

    Here i know i am able to play large video from server through uri instance.But i my objective is play video from sdcard any how?

    Please help me sir and share me your ideas

    thanks,

    ReplyDelete
  11. hello Naveen Kumar,

    You have to play video from SDCard.

    SDCard path will be used on this Application.

    I think its better for you

    Thanks

    ReplyDelete
  12. I cant play the .3gp video file in my emulator.I hav used the above code. I dont know why its not working ? I am getting msg as "sorry this video cant play" . I have used:
    mVideo = (VideoView)findViewById(R.id.videoview);
    Uri path = Uri.parse("android.resource://"+getPackageName()+ R.raw.video);
    mc=new MediaController(this);
    mVideo.setMediaController(mc);
    mVideo.setVideoURI(path);
    mVideo.start();

    ReplyDelete
  13. subrat: you forgot about the "/" in front of R.raw.video

    ReplyDelete
  14. Thanks to all! It works;)

    ReplyDelete
  15. Thanks for your help!

    It worked with the code below.

    Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);

    ReplyDelete
  16. Thank you. This was the missing link for me. Really helpful code sample.


    Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);

    ReplyDelete
  17. Thanks a bunch. It worked when I used
    Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);

    ReplyDelete
  18. Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);
    hey this is not working for me....
    its giving file can not play again and again...
    i have created raw folder inside res and saved the video having file extension mp4. please tell me the fault...

    ReplyDelete
  19. yes, it's working fine for me... Thank for this post, it was very helpful...

    ReplyDelete
  20. can the owner of the post incorporate the comments and make the original post error free?
    also i can hear audio, but cannot see video for mp4 file, with above code.
    same file plays fine on windows with vlc.

    ReplyDelete
  21. Hello Sir,
    Your code it working but i can't see the video only listing the audio.
    please sir help me

    ReplyDelete
  22. Hi, I'm still facing a problem and it is that the audio is working fine but not the video. Its an mp4 file.

    ReplyDelete
  23. Not working!!! I have error "Sorry, this video cannot be played". Please, help me!

    ReplyDelete
  24. I just solved the "Sorry, this video cannot be played" by a simple stupid mistake I had not looked at keenly. I had left the R.raw.video in double qoutes. so "R.raw.video" doesnt work. Problem of just copying and pastig....lolest.....

    ReplyDelete
  25. IT WORK FOR ME BUT THE VIDEO PLAY VERY SLOW

    ReplyDelete
  26. hi there, there are many free video converters on the web. just google it you will find many great result.

    ReplyDelete
  27. why my vidio only sound and the screen only black not a picture???

    ReplyDelete
  28. Your code it working but i can't see the video only listing the audio.
    please sir help me

    ReplyDelete
  29. how to play videos if the video view is on another activity?

    ReplyDelete
  30. Thanks for u r valuable tutorial......
    its working

    ReplyDelete
  31. Thanks! this tutorial helps a lot. had a problem in playing video it says "can't play video" but I realized I saved a zipped .mp4 file in raw :P .. so I extracted it and save unzipped file.. and it works .. thanks!

    ReplyDelete
  32. The method is great, but I think you can convert the raw video to other formats to replay. I have been using abettersoft to convert avi to mov on Mac for enjoy on pc, here is detailed guide:http://www.abettersoft.com/how-to/convert-avi-to-mov-mac.html. It can rip DVD to AVI, MP4, MOV, MKV, WMV, DivX, XviD, MPEG-1/2, 3GP, 3G2, VOB… video formats. have a try.

    ReplyDelete
  33. Post is nicely written and it contains many good things for me. I am glad to find your impressive way of writing the post. learn more about convert mts to mov on mac at http://www.ivideotool.com/

    ReplyDelete
  34. hi I tried everything that is written but I can't find the application when playing it in the emulator it says, [2013-12-28 22:23:30 - Video Testing] emulator-5554 disconnected! Cancelling 'com.android.image.Image activity launch'! and the emulator disconnects

    I don't know what the problem is, can you guys help me :(

    ReplyDelete
  35. how does the video view become full screen. I keep getting a half screen video view.

    ReplyDelete
  36. it worked few days ago but suddenly it says that"can't play video" i checked everything but can't seem to find the problem.what do you guys think the problem is?i badly need to fix this thank you.

    ReplyDelete
  37. getWindow().setFormat(PixelFormat.TRANSLUCENT);
    VideoView videoHolder = new VideoView(this);
    //if you want the controls to appear
    videoHolder.setMediaController(new MediaController(this));
    Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
    + R.raw.your_raw_file); //do not add any extension
    //if your file is named sherif.mp4 and placed in /raw
    //use R.raw.sherif
    videoHolder.setVideoURI(video);
    setContentView(videoHolder);
    videoHolder.start();

    ReplyDelete

How to Free Up Storage Space on Your Android Phone (Without Deleting Photos)

Running out of space on your Android phone is one of the most common (and most annoying) problems out there - especially once you start gett...