我的目标是在服务器上的不同文件夹中存储多个 mp4。我想在满足特定条件时从文件夹中抓取其中一个视频。如何从特定文件夹中抓取随机视频文件?这是我编写的代码:
public static Uri getVideoPath(String cond){
Uri tempPath;
if((cond.equals("01"))||(cond.equals("02"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/sunny/");
}else if((cond.equals("03"))||(cond.equals("04"))|| (cond.equals("05"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/some_sun/");
}else if((cond.equals("06"))||(cond.equals("07"))||(cond.equals("08"))||
(cond.equals("36"))||(cond.equals("37"))||(cond.equals("38"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/cloudy/");
}else if((cond.equals("09"))||(cond.equals("10"))||(cond.equals("27"))||(cond.equals("28"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/light_gray/");
}else if(cond.equals("11")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/fog/");
}else if((cond.equals("12"))||(cond.equals("13"))|| (cond.equals("14"))||
(cond.equals("39"))||(cond.equals("40"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/showers/");
}else if((cond.equals("15"))||(cond.equals("16"))|| (cond.equals("17"))||
(cond.equals("40"))||(cond.equals("41"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/thunderstorms/");
}else if(cond.equals("18")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/rain/");
}else if((cond.equals("19"))||(cond.equals("20"))|| (cond.equals("21"))||(cond.equals("43"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/flurries/");
}else if((cond.equals("22"))||(cond.equals("23"))||(cond.equals("44"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/snow/");
}else if((cond.equals("24"))||(cond.equals("25"))|| (cond.equals("26"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/ice/");
}else if(cond.equals("29")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/rain_and_snow/");
}else if(cond.equals("30")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/hot/");
}else if(cond.equals("31")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/cold/");
}else if(cond.equals("32")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/windy/");
}else if((cond.equals("33"))||(cond.equals("34"))||(cond.equals("35"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/clear/");
}else{
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/clear/");
}
return tempPath;
}
最佳答案
首先,创建一个HashMap,而不是级联的if-else。并 put
条件和路径的每个组合。然后,检索很简单:
public static Uri getVideoPath(String cond){
Uri tempPath = map.get(condition);
if (tempPath == null) tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/clear/");
return tempPath;
}
您无法使用 File 获取目录列表的 listFiles()
因为它仅适用于本地文件。你必须open the URL and read目录列表。假设您的服务器支持 PHP,您可能最好添加一个仅返回文本/纯文本文件列表的 index.php
。将其加载到数组后, Patrick 的其余部分的代码就可以解决问题。
关于java - 如何抓取随机视频在特定条件下播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3187771/