tags:

views:

48

answers:

3

My goal is to have multiple mp4's stores in different folders on a server. I want to grab one of the videos from the folder when a certain condition is met. How would I grab a random video file from a particular folder? Here is the code I have written:

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

You could do something like this:

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];
Patrick
I tried this and posted my code as an "answer" of my own. I don't know if this is the correct way to repost, please let me know.
taraloca
Thanks for your help! I may be back for more later, but for now I am good to go.
taraloca
+1  A: 

First, instead of the cascaded if-else, create a HashMap and put each combination of condition and path. Then, retrieval is simply:

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

You can't get a directory listing using File's listFiles() as that only work on local files. You'll have to open the URL and read the directory listing. Assuming your server supports PHP, you'll probably be better off adding an index.php that simply returns a list of files in text/plain. Once you have that loaded into an array, the rest of Patrick's code will do the trick.

Devon_C_Miller
I'm actually developing this on Android platform. Does this change any of your suggestions?
taraloca
Never mind...I get what you're saying and am working on implementing the HashMap now.
taraloca
A: 

I am trying to get this to work. Here is the method I wrote:

public static File randomVideo(String cond){
        File dir = new File(getVideoPath(cond));
        Random gen = new Random();

        File[] videos = dir.listFiles();

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

        return videos[random_index];

    }

In my original getVideoPath, I returned an Uri. I have now changed it to return a string and with the string:

public static String getVideoPath(String cond){
        Log.d(DEB_TAG, "*********Inside of getVideoPath");
        String tempPath;
        //Uri tempPath;

        if((cond.equals("01"))||(cond.equals("02"))){
            File index = randomVideo(cond);
            Log.d(DEB_TAG, "*********Value of File index is " + index);
            tempPath = ("http://www.personal.psu.edu/tmv105/video/sunny/" + index);} ................more code...........

        return tempPath;
    }

Here is how I am calling this and trying to use it to no avail:

String mPath = LayoutConditions.getVideoPath(wd.getCurrentIconCode());
    mVid.setVideoPath(mPath);  // mVid is my VideoView
    mVid.requestFocus();
    mVid.start();

It's not working. My debug tag doesn't even appear in my logCat inside of getVideoPath method.

taraloca