pydub.playback.play isn't playing audio on windows

69 Views Asked by At

I am using Windows 11 Pro with Python v3.10.11, pyaudio==0.2.13 and pydub==0.25.1 for the playback.
When I try to play the audio it rarely makes a sound and no exception occurs, so in theory it should be playing.
What's also interesting is that when I try to play the file with different players like chrome, windows media player, vlc, etc. it too doesn't play sometimes.
I created the audio file with sfxr and saved it as an .wav. Here is the audio file in question: https://www.mediafire.com/file/nho8ynn7rkp3pq1/test_audio.wav/file

This is my code:

from threading import Thread

from pydub import AudioSegment
from pydub.playback import play


class Audio:
    def __init__(self, file: str):
        self.audio = AudioSegment.from_file(file, file.split(".")[-1])
        self.__thread = None

    def play(self) -> None:
        if self.__thread is None or not self.__thread.is_alive():
            print("Playing audio ...")
            self.__thread = Thread(target=self.__play, args=())
            self.__thread.start()

    def __play(self) -> None:
        play(self.audio)


test_audio = Audio("test_audio.wav")

while True:
    test_audio.play()

I'm expecting it to play the sound over and over again but what happens is that it mostly doesn't play it at all.

0

There are 0 best solutions below