Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to play video from assets?

People also ask

How do I access Android assets?

Step 3 – Right click app >> New >> Folder >> Assets folder. Right click on the assets folder, select New >> file (myText. txt) and your text.

What is the use of assets Folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.

How do I change assets in Android?

Select app/main folder, Right click and select New => Folder => Asset Folder. It will create 'assets' directory in main. Select main folder, Right click and select New => Directory Enter name as 'assets' = > Ok.


Instead of accessing from assests,You must 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();

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

It's AkashG's code, but I remember that R here is not from the Android class. It's from your own project.


You first need to convert your video into the InputStream and then save it to the user's internal storage, then display it and delete that file when the video is finished.

try{
     String path = Environment.getExternalStorageDirectory()+"/"+APP_NAME()+"/videos/"+ls+"/" ;
     InputStream input = getAssets().open("vid/dal.mp4");
     String name = System.currentTimeMillis() +".mp4";
     File f = new File(path);
     f.mkdirs();
     int size = input.available();

     FileOutputStream output = new FileOutputStream(new File(path+name));
     byte data[] = new byte[4096];
     long total = 0;
     int count;
     while ((count = input.read(data)) != -1) {
          output.write(data, 0, count);
          total += count;
          if (size <= total) {
              break;
          }
     }
     output.flush();
     output.close();
     input.close();

     //Toast.makeText(VideoPlayer.this , "file created !" , Toast.LENGTH_LONG).show();

     Uri uri = Uri.parse(path+name) ;

     videoView.setVideoURI(uri);

     videoview.start();

}cath(Exception e){
}