Ask coding questions

← Back to all posts
How do i play a Audio file in Python
h
HrushalNikhare (4)

I have tried reading the docs but it doesn't work ;-;

Answered by SixBeeps (5346) [earned 5 cycles]
View Answer
Comments
hotnewtop
SixBeeps (5346)

Could you share your Repl so that we can see what's happening in your code?

HrushalNikhare (4)

@SixBeeps from replit import audio

name = input('What is your name?\n')
print('Hi, ' + name + ' what would you like to do today:')
input1 = input(' 1.Listen to a song \n 2.Play a game \n')

if (input1=='1'):
print('Great , the song will start in a few moments')

# for playing .mp3 file
source = audio.play_file('Music/Music1.mp3')

elif (input1=='2'):
print('Generating link...')
input2 = input('').lower()

SixBeeps (5346)

@HrushalNikhare You need to keep the program running after you queue the audio for it to play out. Just stick this after you declare the source:

while True:
  pass
HrushalNikhare (4)

Thank you all, for helping me with the Bug i really really appreciate it. Thank you all hope you have a good month :)

Coder100 (18911)

Replit audio is a bit broken, maybe you could implement it manually yourself.

HrushalNikhare (4)

@Coder100

from replit import audio

name = input('What is your name?\n')
print('Hi, ' + name + ' what would you like to do today:')
input1 = input(' 1.Listen to a song \n 2.Play a game \n')

if (input1=='1'):
print('Great , the song will start in a few moments')

# for playing .mp3 file
source = audio.play_file('Music/Music1.mp3')

elif (input1=='2'):
print('Generating link...')
input2 = input('').lower()
if (input2 == 'easter'):
print('ahh yes it works')

ChuuYarxx (0)

first go to your shell and type pip install playsound

and then type in your file

# import required module
from playsound import playsound

# for playing note.wav file
playsound('/path/audio.wav')

i hope this helps. If it doesn't work then idk what to say

SixBeeps (5346)

@ChuuYarxx playsound does not work on Replit. There is a special package for playing sound on Replit, as per the documentation here.

HrushalNikhare (4)

@ChuuYarxx I tried doesn't work on replit

HrushalNikhare (4)

@SixBeeps I already read it yesterday