15

I want to play a video from my assets or raw folder in my app in Android using VideoView I am getting the error as video cannot be played please anyone give me a solution.

Here is the code I used

VideoView vd = (VideoView)findViewById(R.id.Video);         
Uri uri = Uri.parse("android.resource:"  + R.raw.video);
MediaController mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri); 
vd.start();
Jonik
  • 80,077
  • 70
  • 264
  • 372
James
  • 13,891
  • 26
  • 68
  • 93

6 Answers6

33

A few things to note:

  1. You must copy the video into your project's res/raw folder.
  2. It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename: my_video_file.mp4
  3. When you work with this resource in code, you will reference through the generated R statics - it will have the file extension removed: R.raw.my_video_file
  4. The Activity class has a helper method getPackageName() which can be used by your code when constructing the correct URI to your video.
VideoView vv = (VideoView)this.findViewById(R.id.videoView)
String uri = "android.resource://" + getPackageName() + "/" + R.raw.my_video_file;
vv.setVideoURI(Uri.parse(uri));
vv.start();

There is more information on this here.

Darren Hicks
  • 4,946
  • 1
  • 32
  • 35
  • I have used `VideoView` to play video from raw folder, but video is not played in lower version below 2.3. It shows me error "Video cannot be played" please guide me where is the problem,as video plays in other versions. – Dory Sep 30 '13 at 05:41
  • When i rotate screen during play of video , it will start from first. So what i do to prevent it start from beginning ? – Anand Savjani Jul 30 '15 at 07:38
  • @AnandSavjani You can't control that. When rotating the screen, android reloads the current activity – LonelyIdiot Sep 13 '15 at 18:42
  • You can keep track of where you are and pass the information along using the savedInstanceBundle http://developer.android.com/guide/topics/resources/runtime-changes.html – aiden_fry Jan 14 '16 at 09:12
  • The question is to play a video from Assets folder, not from Raw folder. – Ali Raza Apr 19 '22 at 12:58
7

You must include the package name in the uri:

  Uri uri = Uri.parse("android.resource://[package]/raw/video")

or

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

Also, check out these examples.

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
2

There are so many ways to go wrong with VideoView ! Mainly because the logcat gives you no help, always giving error UNKNOWN.

I found this link was by far the best way to get started... A complete description so you can't go wrong. Thanks go to the author...

http://androidexample.com/Play_Video_File_-_Android_Example/index.php?view=article_discription&aid=124&aaid=144

Marky0
  • 1,984
  • 2
  • 12
  • 28
  • The link is dead! Unfortunately, I could strongly relate to what you said! I am struggling with video playback on Android for a while now, what I'm missing is a structured introduction to the topic, the Android Developer Guide doesnt help much here.... Would be great if you could find the content of the link again and update the post. (Could also be that the link is just temporarily unavailable...) – nburk Jun 19 '14 at 08:03
  • Link seems to work for me. If you still have problems google "androidexample.com Play_Video_File_-_Android_Example" – Marky0 Jun 21 '14 at 21:07
0

When we include a video resource in /resource/raw/ or assets/, by default, it looks for the .mp4 format, it won't accept .wmv files. If you read video file from external locations(like: /mnt/sdcard/demo.wmv) then it'll accept them.

user
  • 86,916
  • 18
  • 197
  • 190
naren
  • 1
0

Try:

   AssetFileDescriptor afd = getAssets().openFd(fileName);
   player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
steevoo
  • 621
  • 6
  • 18
0

Make sure to write the file video name without extension.

Pelanes
  • 3,451
  • 1
  • 34
  • 32