tags:

views:

154

answers:

3

I have just few days to show a demo about a Music Player in WPF and i have a trouble that i can not work out right now . I need know how change a location folder meantime the Music Player is running ,i have 3 location folder:

  1. D/: Morning;
  2. D/: Afternoon;
  3. D/: Night;

in each folder there are songs of different genre. This music player will be used everyday from the 8am to 10pm with no stop ,so when run the application in morning(8am) it will download automatically the folder"Morning" but when the timeOfDay is Midday it will change location folder and so pass in the folder"Afternoon"(about 6p would pass to the folder "Night") and at the point i get stuck i don't know how work out this step,i don't know how organise my code to make the Music Player change location folder and download new songs in automatic way. Please do you have some idea to illuminate my mind and go on to finish this Demo? Sorry for my confusion; Thanks a lot

+1  A: 

static public string GetWorkingFolder() { if(System.DateTime.Now.Hour > 1 && System.DateTime.Now.Hour < 12) return @"D:\Morning"; else if (System.DateTime.Now.Hour > 11 && System.DateTime.Now.Hour < 18) return @"D:\Afternoon"; else return @"D:\Evening"; }

would return a differential string dependant on the current pc time (which can of course change) this could then be parsed into a directory or directly used in whatever load method is picking up the various 'tunes'

Matrim
You should check the code formatting button so that your code samples don't look all messed up.
RossFabricant
A: 

Try this. Every time a song ends, before it loads a new one, simply call a function that does this:

    // The string returned is the path
    public string TimeOfDay()
    {
        // How you define 
        if(System.DateTime.Now.Hour >= 8 && System.DateTime.Now.Hour < 10)
            return @"D:\Morning\";
        else if(System.DateTime.Now.Hour >= 10 && System.DateTime.Now.Hour < 18)
            return @"D:\Afternoon\";
        else
            return @"D:\Night\";
    }

If the path returned is differen than the one you already have, then you change, and play songs from the new path.

Marcus L
First Thanks for your response,i need to understand better , if i debug the music player in the morning about 8am this one will load the string Morning after at midday how it can change automatically in the string " afternoon"?Sorry i need to understand how change folder in real time .Thanks
JayJay
Does it really have to change in realtime or simply between tracks? What I mean is, at the strike of 8 will the player switch track, even if it's in the middle of the song? If the track will play until it ends, then it's not a problem and this solution will work.
Marcus L
I added '=' as well to the code, because I realized it wouldn't change until 9 and 11. So use System.DateTime.Now.Hour >= 8 and System.DateTime.Now.Hour >= 10.
Marcus L
+2  A: 

Honsa has the right idea, but this is a slightly cleaner implementation:

public static string GetFolderForTime(DateTime time)
{
  if (time.Hour > 8 && time.Hour < 10)
    return @"D:\Morning\";
  if (time.Hour > 10 && time.Hour < 18)
    return @"D:\Afternoon\";
  return @"D:\Night\";
}

That way you can pass in a time different from the current one if you need to, although normally you would use DateTime.Now.

Also, note that the name of the function describes what it does.

RossFabricant
Thanks,just last question how implement this code or better where i must put it to make the Music PLayer change the folders in right time? ;)
JayJay
Do you mean "How should I call GetFolderForTime?" Maybe you can call it right after a song starts playing so that you have some time to download the next song from the correct folder.
RossFabricant
Can you post a code snippet about that?Thanks in anticipate
JayJay