tags:

views:

49

answers:

2

Hi, I'm developing a small media player in Python. A problem I have run into is that my thread which plays the .wav file never exits. I've provided the thread class, and how I handle the create of thread below.

class myThread (threading.Thread):
        def __init__(self, threadID, wf):
            self.threadID = threadID
            self.wf = wf
            threading.Thread.__init__(self)
        def run(self):
            global isPaused
            global isStopped        

            self.waveFile = wave.open(self.wf, 'rb')            

            #initialize stream
            self.p = pyaudio.PyAudio()
            self.stream = self.p.open(format = self.p.get_format_from_width(self.waveFile.getsampwidth()), channels = self.waveFile.getnchannels(), rate = self.waveFile.getframerate(), output = True)
            self.data = self.waveFile.readframes(1024)

            isPaused = False
            isStopped = False
            #main play loop, with pause event checking
            while self.data != '':
                while isPaused != True:
                    if isStopped == False:
                        self.stream.write(self.data)
                        self.data = self.waveFile.readframes(1024)
                    elif isStopped == True:
                        self.stream.close()
                        self.p.terminate()

            self.stream.close()
            self.p.terminate()

And I control the thread creation with:

    foo = wx.FileDialog(self, message="Open a .wav file...", defaultDir=os.getcwd(), defaultFile="", style=wx.FD_MULTIPLE)
    foo.ShowModal()

    self.queue = foo.GetPaths()
    self.threadID = 1       

    while len(self.queue) != 0:
        self.song = myThread(self.threadID, self.queue[0])
        self.song.start()
        while self.song.isAlive():
            time.sleep(2)
        self.queue.pop(0)
        self.threadID += 1

If you have any idea, I'd appreciate it.

A: 

Is it this code in the thread that never exits? Or the main while loop?

while self.data != '':
    while isPaused != True:
     if isStopped == False:
      self.stream.write(self.data)
      self.data = self.waveFile.readframes(1024)
     elif isStopped == True:
      self.stream.close()
      self.p.terminate()

Just a thought - from what this looks like, surely the while loop will not exit if stopped, since self.data != '' because it has been set by the self.data = line. Just a thought.

blob8108
Yes, that is what is invoked after I create the thread and give the command thread.start()
Michael
but is this where it gets stuck? do you know?perhaps time to do a little debugging with print statements ;)
blob8108
A: 

You need to ensure you set self.data to nothing if isStopped is True.

while self.data != '':
    while isPaused != True:
        if isStopped == False:
            self.stream.write(self.data)
            self.data = self.waveFile.readframes(1024)
        elif isStopped == True:
            self.data = ''
            self.stream.close()
            self.p.terminate()

self.stream.close()
self.p.terminate()
Lee