2010-07-06 59 views
0

我的目标是在服务器上的不同文件夹中放置多个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; 
} 

回答

0

你可以做这样的事情:不是

File dir = new File(getVideoPath(condition)); 
Random gen = new Random(); 

File[] videos = dir.listFiles(); 

int random_index = gen.nextInt(videos.length); 

return videos[random_index]; 
+0

我试过这个,并将我的代码发布为我自己的“答案”。我不知道这是否是转发的正确方式,请告诉我。 – taraloca 2010-07-06 16:45:50

+0

感谢您的帮助!我以后可能会回来,但现在我很好走。 – taraloca 2010-07-06 17:48:38

1

首先,级联的if-else,创造的条件和路径HashMapput每个组合。然后,检索很简单:

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; 
} 

你不能让一个目录使用FilelistFiles()作为本地文件,只有工作清单。你将不得不open the URL and read目录列表。假设你的服务器支持PHP,你最好添加一个index.php,它只是以text/plain的形式返回一个文件列表。一旦你把它加载到一个数组中,其余的Patrick的代码就可以完成。

+0

我实际上正在Android平台上开发这个。这是否会改变您的任何建议? – taraloca 2010-07-06 17:22:12

+0

不要紧......我明白你在说什么,并且正在努力实现HashMap。 – taraloca 2010-07-06 17:47:37