Member Avatar for leegeorg07

hi again, if you remember i had been asking how to extend my music player to do two things:
1)play music from folders and subfolders (complete)
2)change song at the end of each one (incomplete)
well after looking around a while i found the answer :

# create a list of all the files in a given direcory
# and any of its subdirectories (Python25 & Python30)

import os
import os.path

def file_lister(currdir, mylist=[]):
    """
    returns a list of all files in a directory and any of
    its subdirectories
    note that default mylist becomes static
    """
    for file in os.listdir(currdir):
        # add directory to filename
        full_name = os.path.join(currdir, file)  
        if not os.path.isdir(full_name):
            mylist.append(full_name)
        else:
            # recurse into subdirs
            file_lister(full_name)
    return mylist

which i edited and added to my program to make this:

import webbrowser, random
import os
import os.path
running = 1

def file_lister(currdir, mylist=[]):
    """
    returns a list of all files in a directory and any of
    its subdirectories
    note that default mylist becomes static
    """
    for file in os.listdir(currdir):
        # add directory to filename
        full_name = os.path.join(currdir, file)  
        if not os.path.isdir(full_name):
            mylist.append(full_name)
        else:
            # recurse into subdirs
            file_lister(full_name)
    return mylist

dir_name = r"C:\Documents and Settings\Owner\My Documents\\" 
file_lists = file_lister(dir_name)
file_list = filter(lambda x: x.lower().endswith(".mp3"),file_lister(dir_name))

choice = int(raw_input("Press 1 to play a new song"))
while running == 1:
    if choice == 1:
        song = random.choice(file_list)
        webbrowser.open(os.path.join(dir_name,song))
    choice = float(raw_input("Press 1 to play a new song"))

but now i want to make it fully automatic and change song at the end of the previous one, how would i go about this?

What i would do is just sleep the program until the next song is ready, to find out how long to sleep though is the hard thing, so you can either make a dictionary with all the songs and their times. Or you can use something like this:

>>> import mpgedit
>>> play = mpgedit.Play('example.mp3')
>>> play.total_time()
(213, 129)
>>> secs, msecs = play.total_time()
>>> mins = secs / 60
>>> secs = secs - mins * 60
>>> print "%d:%02d minutes" % (mins, secs)
3:33 minutes

from http://simonwillison.net/2003/Dec/4/mp3lengths/

This of course needs a module, that is:

http://www.mpgedit.org/mpgedit/download_sdks.html

It worked fine on my computer so hopefully it will work on yours.

Member Avatar for leegeorg07

thanks i tried that but it keeps on raising this error when i try the test you showed

Traceback (most recent call last):
  File "C:\Python25\testing mpgedit.py", line 1, in <module>
    import mpgedit
ImportError: No module named mpgedit

Did you download and install mpgedit? The link is provided above...

Hold the phone, the actual thing you need to import is pympgedit.
And when you install it you need to log on and off before it works.. changes to the PYTHONPATH

Member Avatar for leegeorg07

thanks i logged of and on again and it works now but like it says in your first link the files only give me

(-1, -1)

do you know the solution to this?

i HAVE googled this to no avail

Hmm i found this with some files too. Does it do that for all files? Or just a couple?

Member Avatar for leegeorg07

i tested it with 13 files and yes it does it with all of them!

Yeah i just double checked and i am getting the same error. Sorry about that, it looked rather promising!

hmm.. if you only have 13 files, then maybe you could make a dictionary with the values of the length.

Member Avatar for leegeorg07

i only tested it with 13 files but ive got at least 3gb of music on my computer that i want to listen to! i also looked at mad but it said it didnt exist! ill take a look at it again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.