使用VideoView播放多个视频

时间:2010-11-19 20:32:36

标签: android

我在矢量{http://a/video1.mp4http://a/video2.mp4http://a/video3.mp4http://a/video4.mp4http://a/video5.mp4http://a/video6.mp4}中保留了一系列视频 我想使用Android原生播放器VideoView一个接一个地播放所有视频。 请提供示例代码以实现相同的功能。

2 个答案:

答案 0 :(得分:0)

在VideoView上设置MediaPlayer.OnCompletionListener。视频完成后将调用您的监听器,因此您可以将视频URI更改为Vector中的下一个。

答案 1 :(得分:0)

package com.example.alok.myapplicationexample;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import java.util.ArrayList;

public class MultipleVideoPlay extends AppCompatActivity {

    private VideoView myVideoView;
    private int position = 0;
    private MediaController mediaControls;
    ArrayList<String> list =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multiple_video_play);
        myVideoView = (VideoView) findViewById(R.id.video_view);
        list.add("https://www.rmp-streaming.com/media/bbb-360p.mp4");
        list.add("https://www.rmp-streaming.com/media/bbb-360p.mp4");
        list.add("https://www.rmp-streaming.com/media/bbb-360p.mp4");
        if (mediaControls == null) {
            mediaControls = new MediaController(MultipleVideoPlay.this);
        }

    mediaControls.setAnchorView(myVideoView);


        try {
            myVideoView.setMediaController(mediaControls);
            myVideoView.setVideoURI(Uri.parse(list.get(0)));
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        myVideoView.requestFocus();
        myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer) {

                myVideoView.seekTo(position);
                if (position == 0) {
                    myVideoView.start();
                } else {
                    //if we come from a resumed activity, video playback will be paused
                    myVideoView.pause();
                }
            }
        });
        mediaControls.setPrevNextListeners(new View.OnClickListener() {
            public void onClick(View v) {
                myVideoView.setMediaController(mediaControls);
               // myVideoView.setVideoPath(list.get(0));
                myVideoView.setVideoURI(Uri.parse(list.get(0)));
                myVideoView.requestFocus();
                myVideoView.start();
                // code for next
            }
        }, new View.OnClickListener() {
            public void onClick(View v) {
                myVideoView.setMediaController(mediaControls);
              //  myVideoView.setVideoPath(list.get(1));
                myVideoView.setVideoURI(Uri.parse(list.get(1)));
                myVideoView.requestFocus();
                myVideoView.start();
                // code for previous
            }
        });
        myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                myVideoView.setMediaController(mediaControls);
                for(String s : list){
                    myVideoView.setVideoURI(Uri.parse(s));
                    myVideoView.requestFocus();
                    myVideoView.start();
                }
                finish();
               // navigateToDashboard();



            }
        });

        myVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {

                return false;
            }
        });
    }



    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        //we use onSaveInstanceState in order to store the video playback position for orientation change
        savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
        myVideoView.pause();
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //we use onRestoreInstanceState in order to play the video playback from the stored position
        position = savedInstanceState.getInt("Position");
        myVideoView.seekTo(position);
    }
}