RSS

Search Engine

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:

Unknown said...

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

Anonymous said...

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.

Anonymous said...

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();

surya said...

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

Raghav Rajagopalan said...

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

Raghav Rajagopalan said...

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

usman said...

doesnt work with assets.

Vinay Julme said...

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

Naveen Kumar shrivastva said...

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.

sawan Modi said...

hello Naveen Kumar,

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

Its easy way for used any video.

Naveen shrivastva said...

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,

sawan Modi said...

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

subrat said...

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();

Golubov Csaba Antal said...

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

Anonymous said...

Thanks to all! It works;)

CDANIEL Araújo said...

Thanks for your help!

It worked with the code below.

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

Anonymous said...

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


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

viru5detected said...

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

sujal said...

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...

Anonymous said...

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

MKMA said...

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.

Nikunj said...

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

mp4 format to mov mac said...

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

Anonymous said...

not working

Anonymous said...

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

Pope said...

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.....

Anonymous said...

IT WORK FOR ME BUT THE VIDEO PLAY VERY SLOW

best keylogger software said...

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

Anonymous said...

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

Anonymous said...

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

Unknown said...

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

Unknown said...

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

Unknown said...

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!

abettersoft said...

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.

videotool said...

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/

Anonymous said...

hi

Anonymous said...

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 :(

Anonymous said...

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

Unknown said...

Thanks!

Anonymous said...

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.

Unknown said...

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();

Post a Comment