[pygame] making a music playlist

[pygame] making a music playlist

Postby metulburr » Mon Dec 23, 2013 12:50 am

So i have like 5 or so mp3 files that i have that are short 1-2 minute long background music pieces. How do you make a playlist to play all of them?

The only thing i seen was using pg.mixer.music.queue() after load to queue in the next song. But that has been unsuccessful for me. It never goes to the next song. The other thing would be hoe to randomize the playlist once you start to get it working?

It appears that you cannot do the same method with music as you can with images....load them in a list, and loop and display them, or rather loop them and play them?

Code: Select all
class Music:
    def __init__(self):
        self.path = 'resources/music'
        self.load()
       
    def load(self):
        for f in os.listdir(self.path):
            end_path = os.path.join(self.path, f)
            abspath = os.path.abspath(end_path)
            pg.mixer.music.load(abspath)
            pg.mixer.music.queue(abspath)

Code: Select all
        self.music = Music()
        pg.mixer.music.set_volume(.1)
       
        pg.mixer.music.play(-1, 0.0)

I messed around with the first arfument of play() but i am not sure how to get it acting like a playlist.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [pygame] making a music playlist

Postby metulburr » Mon Dec 23, 2013 1:58 am

I went with the easiest method, concatenating each mp3 file into one mp3. Although that doesnt help me learn pygame at all. Nor does it allow randomizing the tracks.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [pygame] making a music playlist

Postby Mekire » Mon Dec 23, 2013 12:17 pm

You are going to want to use pygame.mixer.music.set_endevent(). This puts an event of your choosing on the event queue when ever a track ends. You catch the event and flip tracks.

Consider the following global soup which cycles through three sound effects on my hard drive:
Code: Select all
import sys
import pygame as pg


TRACK_END = pg.USEREVENT+1
TRACKS = ["laze.wav","boing.wav","chimed.wav"] #Three sound effects I have
track = 0


pg.init()
pg.display.set_mode((500,500))
pg.mixer.music.set_endevent(TRACK_END)
pg.mixer.music.load(TRACKS[0])
pg.mixer.music.play()


while 1:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()
        elif event.type == TRACK_END:
            track = (track+1)%len(TRACKS)
            pg.mixer.music.load(TRACKS[track])
            pg.mixer.music.play()

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [pygame] making a music playlist

Postby metulburr » Tue Dec 24, 2013 4:16 pm

OK i am still in the process of fully understanding it. I have to read more docs.

I did implement it in, but i am not sure why it is not switching to the next track when its done.

Code: Select all
class Music:
    def __init__(self):
        self.path = 'resources/music'
        self.track_end = pg.USEREVENT+1
        self.tracks = []
        self.track = 0
        for track in os.listdir(self.path):
            self.tracks.append(os.path.join(self.path, track))
        random.shuffle(self.tracks)
        pg.mixer.music.set_endevent(self.track_end)
        pg.mixer.music.load(self.tracks[0])


init
Code: Select all
        self.background_music = Music()
        pg.mixer.music.play()

event
Code: Select all
        elif event.type == self.background_music.track_end:
            self.background_music.track = (self.background_music.track+1) % len(self.background_music.tracks)
            pg.mixer.music.load(self.background_music.tracks[self.background_music.track])
            pg.mixer.music.play()


https://github.com/metulburr/pong

also, on my pc, there is a squeal or a pop from my speakers *sometimes* when i start the game, and then the music flows and then its gone. I am not sure what this is from. Its not from a specific mp3 file as sometimes it will do it with the one song, and then i try it again, and it does not do it again with the same song. This is also not related to your code, as it also happened previously with my concatenated mp3 files.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [pygame] making a music playlist

Postby metulburr » Tue Dec 24, 2013 5:24 pm

OK i figured out the reasoning for why it wont switch. the event handling for it is only in the gamestate, os if you are in the menu at the time of the track ending, there is no handling for it. I was trying to have the music only play while on gamestate, start when you enter the gamestate, and stop when you return back to the menu

I am not sure how you handle upon entering each state?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [pygame] making a music playlist

Postby Mekire » Wed Dec 25, 2013 2:28 am

Write a cleanup method for your state that runs when the state switches to self.done, but before your control switches its state to self.next. Then write a startup method that runs when a state is first switched to.

This is my flip_state method in Control:
Code: Select all
def flip_state(self):
    """When a State changes to done necessary startup and cleanup functions
    are called and the current State is changed."""
    previous,self.state_name = self.state_name,self.state.next
    persist = self.state.cleanup()
    self.state = self.state_dict[self.state_name]
    self.state.startup(self.current_time,persist)
    self.state.previous = previous

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [pygame] making a music playlist

Postby metulburr » Sat Dec 28, 2013 9:00 pm

oh ok. I am still getting use to and learning this structure. I should of known that.

Now i am trying to figure out how to pass music volume levels from control state to game state. I think the only solution is to subclass a tools class, such as you did in PvZ. Although that is going to add another level of complexity to something i still dont feel comfortable with yet.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests