How to play video from raw folder with Android device?

Potato Hwang picture Potato Hwang · Mar 28, 2013 · Viewed 52.7k times · Source

Please help, How to play videos in android device from raw folder for offline mode?

Successful example1: I can play the video from SDcard used the below code.

 Intent intent = new Intent(Intent.ACTION_VIEW);
 String type = "video/mp4";
 Uri uri = Uri.parse("file:///sdcard/test.mp4");
 intent.setDataAndType(uri, type);
 startActivity(intent); 

Failed example2: Question: May I put the test.mp4 to res/raw folder?

 Intent intent = new Intent(Intent.ACTION_VIEW);
 String type = "video/mp4";
 Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.taipei);
 intent.setDataAndType(uri, type);
 startActivity(intent); 

Have anyone can help me? Please.

Answer

AkashG picture AkashG · Mar 28, 2013

Copy the video into your project's res/raw folder. Create raw folder under res folder. It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file.mp4.

VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();

Create videoView in your xml file.