Skip to content Skip to sidebar Skip to footer

Can I Loop Multiple Videos Stored On Raw Folder In Videoview?

What im trying to achieve is to play multiple videos stored on raw folder to be played on loop and sequentially one after the other? I can play only one in loop in videoview but ca

Solution 1:

To play multiple videos located in raw, try the following approach:

(NOTE: take care of the index and your video files naming. This example assumes your videos are named video1, video2..... videoX)

privatefinalintCOUNT=3;
privateintindex=1;
private VideoView myVideo1;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.activity_main);
    myVideo1 = (VideoView) findViewById(R.id.myvideoview);
    myVideo1.requestFocus();
    myVideo1.setVideoURI(getPath(index));
    index++;

    myVideo1.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {
        @OverridepublicvoidonPrepared(MediaPlayer mp) {
            myVideo1.start();
        }
    });

    myVideo1.setOnCompletionListener(newMediaPlayer.OnCompletionListener() {
        @OverridepublicvoidonCompletion(MediaPlayer mediaPlayer) {
                //videos count +1 since we started with 1if (index == COUNT + 1) index = 1;
            myVideo1.setVideoURI(getPath(index));
            index++;
        }
    });
}

private Uri getPath(int id) {
    return Uri.parse("android.resource://" + getPackageName() + "/raw/video" + id);
}

Getting resources from raw explained: android.resource:// is a constant part of the path, getPackageName() points to your application, /raw/ tells the system where to look for the file, video is the constant naming prefix of your files and the id is a dynamic suffix of your file names.

VideoView uses the MediaPlayer for playing videos, here's an overview of its states (taken from the official docs) for better understanding:

enter image description here

Post a Comment for "Can I Loop Multiple Videos Stored On Raw Folder In Videoview?"