25

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.

Potato Hwang
  • 309
  • 1
  • 5
  • 12

8 Answers8

55

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.

AkashG
  • 7,868
  • 3
  • 28
  • 43
  • Hi, I'm getting following problem by using your code: MediaPlayer: Unable to to create media player setDataSource IOException happend : – Usman Rana Dec 01 '15 at 13:10
  • 1
    I already have folder under `res/raw ` but what if I have my video in different resolutions? i.e. 320x240, 640x480, 1280x720 etc.. How I make different folder for each one? – Johny Apr 25 '17 at 12:15
  • This path is not working in Android 10. Do you have any solution for that? – Mohit Rajput Nov 20 '19 at 13:38
10
// 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));
Jeffrey
  • 476
  • 4
  • 13
Shrinithi
  • 326
  • 4
  • 9
  • You should provide some explanations with your code as code only answers tend to be frown upon unless commented enough to be self-explanatory. – Jonathan Drapeau Jul 23 '14 at 12:23
  • for kotlin: val rawId = resources.getIdentifier("file_name_without_extension", "raw", activity?.packageName) val path = "android.resource://" + activity?.packageName.toString() + "/" + rawId videoView.setVideoURI(Uri.parse(path)) login_video.start() – Sadik anass Aug 31 '20 at 20:26
4

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
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
3

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();
Bhaumik Belani
  • 652
  • 1
  • 8
  • 19
1

I struggled with this for dynamic video names. The solution that worked for me was:

//Somewhere set the video name variable
String video+name="myvideo";
//setup up and play video

VideoView videoView=(VideoView)findViewById(R.id.video);
videoView.setVisibility(View.VISIBLE);
String uriPath = "android.resource://"+getPackageName()+"/raw/"+ video_name;
Uri UrlPath=Uri.parse(uriPath);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(UrlPath);

videoView.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mediaPlayer) {
            if (position == 0) {
                try{
                    videoView.requestFocus(); 
                    videoView.start();
                }catch (Exception e){
                    System.out.printf("Error playing video %s\n", e);
                }
            }else{
                videoView.pause();
            }

        }
});

And in XML

<VideoView android:layout_width="300dp"
    android:id="@+id/video"
    android:layout_height="300dp"
    android:orientation="horizontal"
    android:layout_gravity="center" 
    android:keepScreenOn="true"
    />
JanB
  • 904
  • 9
  • 14
1

i think , everyone gave an answer, but doesn't explain the scenario. The main problem here is, If im not mistaken , Android assume that the video coming from your SD Card is dynamic, where in it could be possible , that the format is not supported or supported, thus it enables / ask you to select or open for other third party media software.

While anything you play UNDER RAW folder, requires a handler such as videoview or built in media player which leads to conclusion that anything you put in your RAW folder should be supported / readable by the Android OS.

However , the thread starter here wants that his RAW files , to be played using a third party media player.

jhaypee
  • 71
  • 1
  • 9
0

This Solution will exactly helps you that what you want.

VideoView myVideo;
private MediaController media_control;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myVideo = (VideoView) findViewById(R.id.playVideo);

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

    media_control = new MediaController(this);

    myVideo.setMediaController(media_control);

    myVideo.setVideoURI(uri);

    myVideo.start();

}
0
   player = ExoPlayerFactory.newSimpleInstance(requireActivity(), new DefaultTrackSelector(), new DefaultLoadControl());
   Uri uri = RawResourceDataSource.buildRawResourceUri(getOfflineVideo(offlinePosition));

   MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(requireActivity(),
                "MyExoplayer"), new DefaultExtractorsFactory(), null, null);

   //setup  player with mediaSource
Amirhosein
  • 4,266
  • 4
  • 22
  • 35