我可以循环在videoview的原始文件夹中存储的多个视频吗? [英] 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?
我只能在videoview中循环播放一个,但不能访问其他循环. 提前致谢. 这是我的视频观看.
I can play only one in loop in videoview but cant access other ones. Thanks in advance. Here is my 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);
}
});
}
推荐答案
要播放位于raw
中的多个视频,请尝试以下方法:
To play multiple videos located in raw
, try the following approach:
(注意:请注意索引和视频文件的命名.此示例假定您的视频被命名为 video1 , video2 . ... videoX )
(NOTE: take care of the index and your video files naming. This example assumes your videos are named 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 是文件名的动态后缀.
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
使用MediaPlayer
播放视频,这是其状态的概述(摘录于官方文档)以更好地理解:
VideoView
uses the MediaPlayer
for playing videos, here's an overview of its states (taken from the official docs) for better understanding:
这篇关于我可以循环在videoview的原始文件夹中存储的多个视频吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!