Ask coding questions

← Back to all posts
Morse Key help.
UnikaftUnikaft

Hello repl! First post here. I decided to make a Morse key because I was bored. I didn't want it to translate Morse originally, I planned on somehow making the program be able to determine using length of user input and length in between inputs to determine whether it'd be a (.), a (_), or a (/), however at the moment I can't even get it to play anything.

My main goal at the moment is just to make it play a sound for the duration of a keypress. Eventually I plan on making the screen turn white (not the entire screen, just the one in the top right for pygame) as opposed to black by default, but my main focus is just the sound for the moment.

I tried making it play a note, guided by replit's own guide on playing audio. That didn't work, at least it wouldn't play again. Then tried audio files. Didn't like how short it was, so I made it longer and tried to make it loop while pressed, and play a loopoff sound when it stopped (not a problem at the moment, as I haven't even been able to get there to test it!)
Always the same error, regardless of keypress length, tine.sleep length, or audio file length:

Voters
UnikaftUnikaft
Comments
hotnewtop
JBloves27

So currently, I have tried as well, replit audio doesn't work well. I have tried multiple times, and yet it doesn't work. You should try using a different module or system, or maybe use a different python lib to access music.

Hope this helps!

UnikaftUnikaft

@JBloves27 Thank you! (I've also noticed numerous problems with replit's audio function lol)

JBloves27

No problem! (lol yes) @UnikaftUnikaft

CodeMaster007

Yes. That is because replit runs on servers and not on your build in cpu. That is why you can run repl off a ipad or phone. If you were to set up a camera off regular code, then you would not be able access the camera because it is built in the servers. You can do it with html however because the main website or domain runs off your regular cpu. @JBloves27

CodeMaster007

Hey! So in your main code you never stated a place where the sound was supposed to execute. So basically to fix this, where you want sound, just type this..

pygame.mixer.music.load("testloop.wav")
pygame.mixer.music.play()
pygame.event.wait()

Its simple but I don't see this anywhere in your code.

CodeMaster007

UnikaftUnikaft

@CodeMaster007 Would it work to load it earlier, time.sleep a few seconds, and play it when needed? And how do I pause or stop it when the user releases the key?

CodeMaster007

So the thing I said above plays it for the full value and then stops. If you want to make it play for 1 second instead of 4, you can make a new file and include a testloop1.wav or 2. This time, how long the sound will be is 1 second. @UnikaftUnikaft

UnikaftUnikaft

@CodeMaster007 Would that allow it to loop while the key is held?

CodeMaster007

Uh, I'm not sure but I think so. Test it out and if it works, then yes == true @UnikaftUnikaft

UnikaftUnikaft

It seems to not want to co-operate. The code seems to work, but the audio does not play and it doesn't encounter any errors. Take note I've put a quick

at the beginning to trigger the little "We hear audio!" popup in case that is part of the problem.
@CodeMaster007

CodeMaster007

I don't think so but maybe the "we hear audio" is the thing that is messing up the code so I would copy the code, delete it and see if it works. If it still is not working, then put the copied code back in and I'll look into this. @UnikaftUnikaft

UnikaftUnikaft

@CodeMaster007 I made that code a multi-line comment (using what else, py '''comment''') and it still has the same problem. I've taken out the triple quotes and leaving it as is for now.

UnikaftUnikaft

How'd it go? @CodeMaster007