Last updated 10 months ago
I'm trying to add music to my sketch but it's unfortunately not playing.
Here is my code:
music = loadSound("/samples/music/we-are-haileybury-8-bit.mp3")
playSound(music, loop=True, volume=0.7)
It doesn't come up with an error - it just doesn't play anything. I have tried this signed in, signed out, on incognito mode, in MS Edge and in Chrome. Every time it produces the same result.
Is there something I'm doing wrong? Is it in the wrong place in my code?
Hi Haveesh,
Thanks for the question. The problem seems to be that as soon as the program ends, any music that is playing also ends. Hence if you only have the two lines as demonstrated in your original question, then the program will end almost instantly and hence the music will stop immediately. If you add a new line:
sleep(10)
then your program will keep running for 10 seconds and the music will play for 10 seconds. Normally if we write a program with music we will have some kind of loop that keeps the program going and hence the background music will continue. Let me know if this solves your problem.
Become a free member to post a comment about this question.
Haveesh Surendran Posted 1 year ago
Yeah, that was the issue - I assumed the program would wait for the music to finish playing and then stop, but the program has to keep running for the music to play.
I was able to solve the problem by adding a while loop and an option for the user to quit the program manually.
Thanks!