Pygame wont play background music when loading game - python

Recently I have been trying to make a game in python with pygame and have so far succeeded. My endeavors came to a sudden halt when I tested the game after implementing sound code, and found that no sound played. I have looked around the forums and most people say to convert the .mp3 to a .wav or .ogg, and convert I did, but to no avail. I was thinking if it isn't the file, then maybe there must be something off with my code. Can you guys tell me what I can do to fix it?
The code is as follows
pygame.mixer.init()
pygame.mixer.music.load('BennyHill.wav')
pygame.mixer.music.play(-1, 0.0)
I am just a beginner at the moment, so it might look completely wrong to you experts out there

Do pygame.music.play(-1). In my programs that always works.
The docs are sometimes incorrect with needed/optional arguments.

Related

How do I play a game I've coded on pycharm using pygame onto retropi?

Good evening,
I was wondering how to move a game from pygame to retropi software to be used with the picade? Also, how would I incorporate the game controls (buttons instead of keys) in my program to assure it is compatible if I can move it over.
Many thanks,
Quantum Cat
I have researched into uploading games using a USB in ROM, not sure if I could do this with my own code? Im a little bit of a newbie :)

Pygame mixer.music can't read mp3 stream

I am trying to make my own music player with Python, and after looking at alternatives, I've settled on using pygame's mixer.music to actually play the audio. (I've used pygame before, just usually for actual games) I was looking at playsound instead until I realized I needed a way to play the next song once one is done, as well as the ability to play and pause the audio. I also need to play mp3 files instead of the wavs that most alternatives require.
I actually got it working perfectly originally, until I tried adding other unrelated features, and now it's saying:
File "main.py", line 66, in playCurrentSong
mixer.load(path.join(museDir, currentSong))
pygame.error: Error reading the stream. (code 18)
(museDir is my variable for the directory that music files are in, and mixer is my variable for pygame.mixer.music as a shorthand)
I cannot figure out for the life of me why it's giving me this error now, as it played the audio perfectly fine before. My code is here: https://pastebin.com/V7nAfmK6
If a solution only works on a certain operating system, my final OS will be Linux, on a Rasperry Pi, but I'm trying to write and test the code on Windows. However, if that's not possible, I understand.
Thank you beforehand for any and all help; this is giving me a headache.
I just found the source of the problem.
Before the error popped up, I had been trying to mess around with the metadata of the mp3 files in order to incorporate a genre system into the player, but nothing was working. I eventually decided to use csv files for that instead.
I must have done something wrong however when I was messing around with that metadata because I looked at the mp3s in File Explorer, and they were all 0 bytes. That's why pygame couldn't read the stream: there wasn't one! I plugged the pygame stuff back in, replaced the mp3s with new ones, and it works just fine now.
Thanks for the help anyway though, Torxed!

Key repeat in browser from python code

Note : I am pretty surprised I did not see that same question on this forum, if I missed it please link in comment :)
I am writing a bot in python that would eventually be able to play a browser-based video game (HTML5 canvas game, not Flash). In order to do that I need to simulate the keyboard events from the python code.
I thought pyautogui would do the work, but unfortunately it seems the only way to repeat a key is to do pyautogui.press(key) in a loop, and it is not good at all for playing games as the move is not smooth at all.
I found some solutions for Windows, but I need a solution for macOSX.

Pygame Audio Stops When Window Switches?

I wrote a music player in Python using Pygame and Pydub(I used Pygame for actually playing the music, while Pydub is probably unrelated to the issue).
The music works fine even when windows are switched unless I switch to another Pygame window. I thought this effect would go away if I compiled it(cx_freeze), but that didn't work.
So I was wondering if there is any way to let the music keep playing when the window is switched to another Pygame window.
I used pygame.mixer.music instead of Sound objects if that might somehow be related.
Thanks in advance!
Not exactly a fix, but it seems that if I disable the mixer(pygame.mixer.quit()) it doesn't cancel out the audio. Unfortunately, I haven't found a way to use audio in two pygame windows at once.
Feel free to post another answer if anyone figures out a full fix.

Sound on [every global] keypress (linux)

im trying to find/write a simple daemon in python (preferably) to play a sound on every key pressed, for usability purposes. Something like this https://www.grc.com/freeware/clickey.htm but for Linux.
Ive found some codes around but still cannot put them together. If you guys could point me in the right direction, it would be great
Thanks for your help.
I think this will give you easiest work around:
http://www.speech.kth.se/snack/
After this you can use following code:
s = Sound()
s.read('mymusic.mp3')
s.play()
Good thing is that it works on both windows and linux.
I've found the Qwertickle typewriter sound scheme pretty easy to learn and modify for own needs. Here is my version with pc keyboard sounds :)

Categories