我可以在videoview中循环存储在原始文件夹中的多个视频吗?

时间:2016-03-03 15:04:35

标签: android android-videoview

我想要实现的是播放存储在原始文件夹中的多个视频,以便循环播放并依次循环播放?

我在视频视图中只能播放一个但不能访问其他视频。 提前致谢。 这是我的视频观看。

private VideoView myVideo1;
String path = "http://192.168.0.22/output/files/video/";
Uri uri=Uri.parse(path);

@Override
public void onCreate(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.setVideoURI(uri);
    myVideo1.start();
    myVideo1.requestFocus();

    myVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });
}

1 个答案:

答案 0 :(得分:2)

要播放位于raw的多个视频,请尝试以下方法:

注意:处理索引和视频文件命名。此示例假设您的视频名为 video1 video2 。 ... videoX

private final int COUNT = 3;
private int index = 1;
private VideoView myVideo1;

@Override
public void onCreate(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(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            myVideo1.start();
        }
    });

    myVideo1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
                //videos count +1 since we started with 1
            if (index == COUNT + 1) index = 1;
            myVideo1.setVideoURI(getPath(index));
            index++;
        }
    });
}

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

raw获取资源说明:android.resource://是路径的常量部分,getPackageName()指向您的应用,/raw/告诉系统在哪里查找文件,video 是文件的常量命名前缀,id 是文件名的动态后缀

VideoView使用MediaPlayer播放视频,此处概述了其状态(取自official docs)以便更好地理解:

enter image description here