Hello.
My question is how can I play one file at a time with pygame.I used
the for loop and     it didn't work.
Any help please.
# -*- coding: utf-8 -*-
import wx
import os
import time
import pygame 
from pygame.locals import *




started = False
playing = True
old_track = ''

pygame.mixer.pre_init(44100,-16, 2, 1024 * 3 )
pygame.init()
pygame.mixer.music.load('7.mp3')

def holder():
        time.sleep(0.001)

def play(track):
        global old_track, playing, started  
        MUSICENDEVENT = USEREVENT 
        t = wx.PyTimer(holder)
        if track == old_track:
                pygame.mixer.music.set_endevent(MUSICENDEVENT)
                for event in pygame.event.get():
                        if event.type == MUSICENDEVENT :
                                pygame.mixer.music.load(track)
                                started = False 
                                playing = True

                if not started:
                        #print 1
                        pygame.mixer.music.play()
                        playing = True
                        started = True
                elif playing:
                        pygame.mixer.music.pause()
                        playing = False
                else:
                        pygame.mixer.music.unpause()
                        playing = True
        else:
                pygame.mixer.music.load(track)
                pygame.mixer.music.play()
                started = True
                playing = True
                old_track = track 
        t.Start(10)

def rewind():
        pygame.mixer.music.rewind()

class Test(wx.Frame):
        def __init__(self, parent, id, title):
                super(Test, self).__init__(parent, id, title)
                global track 
                track = ''
                p = wx.Panel(self,-1)
                mainSizer = wx.BoxSizer(wx.VERTICAL)

                buttonsSizer = wx.BoxSizer(wx.HORIZONTAL)
                self.b1 = wx.Button(p,-1,u'ok')
                self.b1.Bind(wx.EVT_BUTTON, self.OnOk)
                self.b2 = wx.Button(p, -1, u'rewind')
                self.b2.Bind(wx.EVT_BUTTON, self.onRewind)
                buttonsSizer.Add(self.b1, 0, wx.ALL, 3)
                buttonsSizer.Add(self.b2, 0, wx.ALL, 3)
                mainSizer.Add(buttonsSizer, 0, wx.ALL|wx.EXPAND, 5)
                p.SetSizer(mainSizer)
                self.Show(True)

        def OnOk(self,evt):
                t = wx.PyTimer(holder)
                files = ['1.mp3', '2.mp3', '1.mp3']
                for i in files :
                        play(i)
                        #while pygame.mixer.music.get_busy():
                                #time.sleep(0.05)
                #t.Start(10)

        def onRewind(self, event):
                rewind()

app = wx.App()
Test(None,-1,'frame')
app.MainLoop()

Reply via email to