Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play video from raw folder with Android device?

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.

like image 714
Potato Hwang Avatar asked Mar 28 '13 06:03

Potato Hwang


People also ask

How do I play a raw video folder?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create a new android resource folder(raw) and copy-paste your video file in that folder.

Where is the raw folder on Android?

The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw.


4 Answers

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.

like image 104
AkashG Avatar answered Oct 13 '22 13:10

AkashG


// To get files from any resource folder (eg: raw, drawable, etc.)
// Use the resource id
int rawId = getResources().getIdentifier(file_name_without_extension,  "raw", getPackageName());

// URI formation
String path = "android.resource://" + getPackageName() + "/" + rawId;

// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path));
like image 29
Shrinithi Avatar answered Oct 13 '22 13:10

Shrinithi


Check this solution How to play videos in android from assets folder or raw folder?

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
like image 4
duggu Avatar answered Oct 13 '22 15:10

duggu


in my code "applicationdemo" is the the name of my video file.

    String video_url = "android.resource://" + context.getPackageName() + "/" + R.raw.applicationdemo;
    final VideoView videoView = findViewById(R.id.dialog_video);
    Uri videoUri = Uri.parse(video_url);
    MediaController mediaController= new MediaController(context);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(videoUri);
    videoView.requestFocus();
    videoView.start();
like image 2
Bhaumik Belani Avatar answered Oct 13 '22 14:10

Bhaumik Belani