Android: how to play video from assets?
Asked Answered
O

6

37

I am making an application in which I have to show video from assets folder in a Fragment. Can anyone help me do this? Do I need to use VideoView in XML?

Olvera answered 6/7, 2012 at 5:55 Comment(3)
possible duplicate of How to play videos in android from assets folder or raw folder?Wheelhorse
Yes it is duplicate but do check the link again none of the code is workingOlvera
have a look at this issue #3746861Brine
B
88

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();
Bracknell answered 6/7, 2012 at 6:5 Comment(4)
Your activity extends Fragment or FragmentActivity.Pls Check this.Bracknell
Hello,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.Carnarvon
Hey Nidhi, what is format of the video? Can u pls check the video formats supported below 2.3. Otherwise above example works perfectly to play video from asset folderBracknell
try this link for mp4 in asset directory binpress.com/tutorial/video-cropping-with-texture-view/21Bearden
S
3
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.

Sage answered 17/10, 2014 at 8:48 Comment(0)
B
2

you can use the MediaItem to play a video from assets

    val playerView = view.findViewById<StyledPlayerView>(R.id.player)

    val player = ExoPlayer.Builder(context!!).build()
    
    val videoUri = Uri.parse("asset:///test.mp4")
    val item = MediaItem.fromUri(videoUri)

    player.addMediaItem(item)
    playerView.player = player

    player.prepare()
Bustee answered 1/2, 2023 at 13:3 Comment(0)
C
0

I have already suffered from same problem, u should prefer project's res/raw folder instead of assets. Create raw folder under res folder. Save video file in a supported format (3gp, wmv, mp4 ) and named with lowercase, numerics, underscores and dots in its filename likewise:filename.3gp into the raw folder.

VideoView videoview = (VideoView) findViewById(R.id.VideoView);

String uriPath = "android.resource://your application package name/raw/your 
wmv/mp4/3gp file in res/raw path without extension";

videoview.setVideoURI(Uri.parse(uriPath));

videoview.start();
Constantine answered 5/12, 2017 at 11:6 Comment(0)
K
0

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){
}
Kassala answered 15/6, 2019 at 12:3 Comment(0)
T
-1

Playing Video(sample.mp4) present in res/ raw folder, along with the Media Controller

// Import Statements

import android.widget.VideoView;
import android.widget.MediaController;

public class youractiviy extends Activity {

private VideoView videoView;
private MediaController mediaController;

protected void onCreate(Bundle savedInstanceState) {
 // Your Startup code
 videoView = (VideoView) findViewById(R.id.video_view);
 videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.sample); 
 mediaController = new MediaController(TestActivity.this);
 mediaController.setAnchorView(videoView);
 videoView.setMediaController(mediaController);
 videoView.start();

}
}

// XML Code

<VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Tremulous answered 23/5, 2018 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.