my music player

Thread Solved

Join Date: Jul 2008
Posts: 393
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 30
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

my music player

 
0
  #1
Feb 6th, 2009
i have made a simple music player that will randomly choose a song from the list (music)
how can i expand this to a whole directory of music without having to make a list with them all in
heres the code:
  1. import webbrowser, random
  2. music = [#music was here]
  3. while 1:
  4. choice = float(raw_input("Press 1 to play a new song"))
  5. if choice == 1:
  6. song = random.choice(music)
  7. webbrowser.open(song)
Last edited by leegeorg07; Feb 6th, 2009 at 6:30 am. Reason: didnt attach the file
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: my music player

 
0
  #2
Feb 6th, 2009
I think Python cannot pick a random file in a directory. If that is the question....
Maybe you can walk through the files, and after a random time you play the current music. But that does not make much sense for me.

Is the music directory too large?

I would recommend a more straightforward method, first without recursion.

  1. import webbrowser, random
  2. import os
  3. import os.path
  4. DIR="your directory containing mp3- files"
  5. music = filter(lambda x: x.lower().endswith("mp3"),os.listdir(DIR))
  6. while 1:
  7. choice = int(raw_input("Press 1 to play a new song"))
  8. if choice == 1:
  9. song = random.choice(music)
  10. webbrowser.open(os.path.join(DIR,song))
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,356
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 125
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: my music player

 
0
  #3
Feb 6th, 2009
Isn't file list a string? Then I guess it should be able to play that. But here is the way I suggest, instead of using directly file names, use indices and random module:
1. Load all paths into list
2. Load random module
3. After music has ended, use random module to select a random index
4. play the file whose index is chosen

here is the random module issue

http://www.google.co.tz/search?q=pyt...ient=firefox-a
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 393
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 30
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: my music player

 
0
  #4
Feb 6th, 2009
thanks it worked but i needed to add
  1. music = list(music)
otherwise it was a string like estevemd said

this is my code now
  1. import webbrowser, random
  2. import os
  3. import os.path
  4. DIR="C:/Documents and Settings/Owner/My Documents/georges poopy/main music"
  5. music = filter(lambda x: x.lower().endswith("mp3"),os.listdir(DIR))
  6. music = list(music)
  7. while 1:
  8. choice = int(raw_input("Press 1 to play a new song"))
  9. if choice == 1:
  10. song = random.choice(music)
  11. webbrowser.open(os.path.join(DIR,song))
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC