循环播放多个视频
Play multiple vidoes in loop
您好,我想循环播放多个视频。我正在尝试这段代码:
private VideoView videoView = null;
String[] videoArray = {"video1", "video2"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[0]);
videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoURI(videoUri);
videoView.start();
videoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp)
{
Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[1]);
videoView.setVideoURI(videoUri);
videoView.start();
}
});
}
}
但是这段代码只循环播放最后一个。
试试这个
假设您的视频名为 video1、video2..... videoN。
只关注索引。
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);
}
您好,我想循环播放多个视频。我正在尝试这段代码:
private VideoView videoView = null;
String[] videoArray = {"video1", "video2"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[0]);
videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoURI(videoUri);
videoView.start();
videoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp)
{
Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[1]);
videoView.setVideoURI(videoUri);
videoView.start();
}
});
}
}
但是这段代码只循环播放最后一个。
试试这个
假设您的视频名为 video1、video2..... videoN。
只关注索引。
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);
}