Skip to content
← Back to Community
Play sound in a repl with Python
Profile icon
rodrigogiraoserrao

Hello everyone,

In light of the new challenge I was going to create something with Python (and pygame) and it would involve playing sounds, but turns out I cannot use the pygame.mixer module. Is it because I am a n00b?
Either way, is there any (relatively simple) way of playing audio in a Python repl?

Thanks!

Answered by 21natzil [earned 5 cycles]
View Answer
Voters
Profile icon
FINNALMSTEDT
Profile icon
AaronWang35
Profile icon
JaydenOsterlund
Profile icon
SadieMalone
Profile icon
Rohan12345123
Profile icon
CodingIsEpicYEE
Profile icon
EthanMartin223
Profile icon
ArkMart
Profile icon
prateeknair10
Profile icon
DanielSitze
Comments
hotnewtop
Profile icon
21natzil

Sorry, but repl.it does not support sound at the moment. Sorry for the inconvenience. This feature is planned and will be added in time.

Profile icon
rodrigogiraoserrao

@21natzil Ok, then there is something I am missing; this music box has sound when I play it! But you said that repl.it does not support sound, so what is the distinction here?

Profile icon
21natzil

@RojerGS When creating a website on repl.it, you're only restricted by your browser. When working with Python / Pygame repls, sound is not supported, sorry for the confusion.

Profile icon
flaryx32

@21natzil 2years and still nothing done?

Profile icon
MarkusStepanovs

I needed an alert tone for a project and found a very ugly solution:

print ('\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a')

But hey, it works!

Profile icon
Tyrannicodin

@MarkusStepanovs how does it work?

Profile icon
alan_oregano

@finlaycameron06 It takes advantage of the "\a" bell escape sequence which makes a sound in the repl.it python terminal
https://en.wikipedia.org/wiki/Bell_character

Profile icon
TanayGupta1

@MarkusStepanovs It doesn't work for me. Like it doesn't play the sound. Did they change it so it doensn't work anymore cuz it has ben 7 months since u said this.

Profile icon
TanayGupta1

@MarkusStepanovs It doesnt work for some reason. How do I make it work?

Profile icon
ClassicShak

works just fine for me!!
@MarkusStepanovs

Profile icon
atharva12345

please insert the audio system because I loved repl.it so much and don't wanna quit this by just 1 excuse please add it as soon as you can.
Thanks.

Profile icon
chough

It seems that they have built an explorer feature to solve this! More info here.

Profile icon
Hidere0225

cool. hope it turns out good bro

Profile icon
26pwong

@MarkusStepanovs Instead of printing it repeatedly, you can use a loop.
This is what I made:

import time

def beep():
print ("\a")

for i in range(10):
beep()
time.sleep(0.1)

It works, but it prints a new line every time it plays the sound. Anyone know a better solution?

Profile icon
alan_oregano

@26pwong

import time def beep(interval = 0.1, frequency = 10): for i in range(frequency): print ("\a", end = "\r") time.sleep(interval) beep()

This sets the end argument of the print statement to a carriage return escape sequence.
Moving it back to the start of the line instead of a new one by default.

You could also add parameters to the function to control the interval and frequency of the bell/alert tone.

Profile icon
ariall

pl