Pygame Audio Stops When Window Switches? - python

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.

Related

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 wont play background music when loading game

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.

Can you get Pygame to work properly on retina screens?

I have the same problem as here: Screen displays only in top left corner of window
The pygame container is messed up by the high resolution retina screen. I have searched around and haven't found any one with an answer other than "change your resolution".
Is it possible to fix this in the game, so that the player doesn't have to fix it themselves? Either fixing the bug in pygame or resizing the resolution when the game starts? Maybe it could work only in fullscreen somehow?
TO CLARIFY: I don't want to have to change the screen resolution manually, but within the pygame-code.
I don't know if you've figured this out at all yet as it's been a while. But you can enable low resolution mode for the pygame window in the Macs application settings. Then you don't have to change your resolution every time. I still haven't figured out how to fix this in code though.
https://support.apple.com/en-us/HT202471
Edit: I'm assuming it's a Mac you're using.
I am afraid there isn't any way. pygame hasn't been updated for a while and I think development has stopped. I ran into the same problem and ended up dealing with using a program called setresx when necessary. The problem can also be fixed by hooking up your computer to an external monitor. I know you can get some portable ones that you can put on the side of your screen.
As to changing the code, as far as I'm concerned it isn't pygame that there is anything wrong with, but technically the screen. That said, you might be able to change the library itself to handle the issue.

How to accept text input from a pygame GUI

I am working on a RPG game using Python and Pygame, and am trying to make a two-part GUI, including a lower part that is like the basic command line, and a top part that will show all graphical "action."
What I need to find out is a way to include both in a pygame window, instead of using a pygame window and terminal window. As well, are there any Pygame GUI toolkits that would be appropriate for this use?
Thanks All!
May I suggest using ezText instead?
It's a cool way to add text inupt bars to pygame. I used it before my self, and It's really easy to use.
http://www.pygame.org/project-EzText-920-.html
(feel free to leave a comment if you want help using it, although everything you need to know is in the example.py that comes with it)
Take a look here (http://wiki.python.org/moin/PythonGameLibraries) for a whole list of ToolKits for both Pygame and Pyglet. Albow (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Albow/) has worked well for me in the past.
The easiest way to accomplish what you're talking about would be to make two Surfaces, one for each part of the interface, and then constantly update them in separate modules to finally blit them every frame. That way your main module can be simplified to something like:
import action_gui
import cl_gui
import pygame
pygame.init()
MAIN_SURF = pygame.display.set_mode((x, y))
pygame.display.set_caption('My Game')
while (True):
action_surf = action_gui.update()
cl_surf = cl_gui.update()
MAIN_SURF.blit(action_surf, my_position_1)
MAIN_SURF.blit(cl_surf, my_position_2)
Best of luck.

Categories