Thread stops running when creating an AudioSegment from pydub

267 Views Asked by At

I'm creating an application that will play audio on a different thread. The thread runs fine until it reaches the statement where I create an AudioSegement from the pydub library. I want it to continue executing after this statement so that the audio will play in the background.

def add_stream_queue(self):
     search_result = self.search()
     stream = Stream(search_result.get_title(), search_result.get_link())
     self.stream_queue.append_queue(stream)
     if self.first_stream:
         self.stream_thread = threading.Thread(target=self.stream_queue.play_stream_queue, args=[self.play_audio])
         self.stream_thread.start()

Here is where I create the thread and it calls the function play_stream_queue() which is in the class StreamQueue.

def play_stream_queue(self, play_audio):
    while self.current_stream < self.get_size():
        self.first_stream = False
        self.manage_stream.download_stream(self.get_stream(self.current_stream).get_link())
        play_audio.start(self.get_stream(self.current_stream).get_title())
        self.current_stream += 1
        print("reached")
    self.first_stream = True

In this function I start to play the audio in the line play_audio.start(...). The thread executed perfectly at this point.

def start(self, filename):
    self.filename = self.STREAM_FOlDER + filename + ".m4a"
    self.stream_file = AudioSegment.from_file(self.filename, format="m4a")
    print("TEST")
    # Create an interface to PortAudio
    self.p = pyaudio.PyAudio()
    .
    .
    .

In the PlayAudio class is where the thread is giving me issues. The thread will run fine until it reaches the statement,

self.stream_file = AudioSegment.from_file(self.filename, format="m4a")

The thread will not continue after this statement.

The odd part is that it used to work perfectly fine and the thread would continue executing after this statement but then it randomly stopped working. I also notice if the program has an error and crashes the stream_thread will start playing.

0

There are 0 best solutions below