1

I want to play sound through my python script:

I have used win_sound pyglet playsound and pygame but it doesn't want to work.

Is there any way to do this.

the directory is C:Users/Random/Folder/OtherFolder/Sound1

2 Answers2

1

You can use librosa for loading audio data and sounddevice for playing audio.

Like this:

import time
import librosa
import sounddevice as sd


def play_audio(audio_path, sampling_rate=44100):

    audio, sampling_rate = librosa.load(audio_path, sr=sampling_rate)
    
    duration = librosa.core.get_duration(audio, sr=sampling_rate)
    
    # play the audio
    sd.play(audio, sampling_rate)
    # wait until the audio is done playing
    time.sleep(duration)



play_audio('./test.mp3')

Moreover, if this function causes the rest of your program to freeze ( because of time.sleep ) you need to run the play_audio function on a separate thread like here.

0

Try using the playsound module.

You need to install it first (enter in command prompt):

pip install playsound

And then you can use it as shown:

from playsound import playsound
playsound("C:Users\Random\Folder\OtherFolder\Sound1.wav")

(Replace the ".wav" thing with whatever file extension you have it set to).

AngusAU293
  • 19
  • 7