I have been trying to run sound in background while App is running. I am Arch Linux and Python 3.8
I have tried playsound as like this
playsound('music.mp3', False)
but I get error saying system not supported. I have also tried pygame following way:
from pygame import mixer
mixer.init()
mixer.music.load("music.mp3")
mixer.music.play()
But I get error pygame.error: Unrecognized audio format. Is there any other way I can run music in background some task is executing on GUI with tkinter. The program will run on arch and Ubuntu.
Try this code it will run the background music continuously till the app is running
mixer.music.play(-1)
Try this:
mixer.music.play(-1)
And if you want your background music to be stopped in 10s then add this too:
gui_name.after(10000, mixer.music.stop)
Hope it will help you.
Related
I am trying to play a video file using vlc media player through the subprocess command.While running the python3 source code in pycharm, video is played without any console blink. If I convert it to exe and run the application, I am able to see the console blinking, before the video is started to play. Video is played only after a while
(video is played only after console blinks, delay is seen).
I seem to be having difficulty with executing a pygame program on boot up. I have a raspberry pi 3 model B running the latest versions of the required libraries. I am simply trying to play a wav file through pygame when a button is pressed. I substituted an “led.on” script for the pygame to ensure my program was being started on boot-up and it ran flawlessly. However, when I try to use pygame on boot up it will not play the audio file. Although pygame will play the file in the terminal, it will not play on startup.
I am using crontab to run the program from boot. Using the following line:
#reboot python /home/pi/test.wav &
And the python code:
from gpiozero import Button
import pygame.mixer
pygame.mixer.init()
song=pygame.mixer.music.load(‘/home/pi/test.wav’)
btn=Button(27)
while True:
btn.when_pressed=(pygame.mixer.music.play)
Any help would be greatly appreciated, thank you!
This should solve your question
pygame.mixer.music.play()
play should be called with () as it's a fn
use
pygame.mixer.music.play(-1)
for looping the music
This is in regards to the book "Python Crash Course 2nd Edition".
After going through the "Drawing the Ship to the Screen" section in Chapter 12, I get a black screen, instead of a grey screen, and I'm not seeing the ship show up when I run my alien_invasion.py. I have tried running Matthes' downloadable resource file for that step, and I still get a black screen. I'm running these .py files from Sublime text, but have tried using terminal to run them (I get indent errors) and python IDLE to run them (gives me a pygame module not found error, though I know pygame is installed and found by Sublime).
Here is the code for the game that should no display a grey background and a ship at the bottom of the screen, if you have the ship image:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game."""
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
Im using Mac OS version 10.14.6.
Any solutions for this issue, or better practices or programs that I should be writing/running these game modules in?
Anyone with a Mac that has successfully installed pygame and/or has gotten alien_invasion to work?
One reply to my reddit post about this said they had a similar issue on Mac OS but when they tried on linux it worked fine...
Is there another alternative for Mac people to use short of installing linux on a separate partition or something?
Thanks for your help!
Found a solution on Eric Matthes's github. Uff da, pygame is not stable on python 3.8.2 yet...I needed to install the dev version of pygame to run with python 3.8.2.
From Eric's github:
The stable version of Pygame has not been updated to work with Python 3.8 yet. However, there is a recent development version that works with Python 3.8. To install it, run the following command:
$ python -m pip install pygame==2.0.0.dev6
You should use the same command you use to run a Python terminal session on your system, which might be python, python3, py, python3.8, or something else.
If you’ve had any issues running Pygame on macOS, this version of Pygame should address those issues as well.
I had the same issues, black screen and no ship. What solved the issue for me was, under settings, playing with the height and width. I'm a novice at this but I found that when I centered the ship instead of bottom center, the ship barely showed up at the bottom. Eventually I was able to center it at the bottom of the screen.
I had this issue with the black screen closing immediately after. The code has the ship in folder 'images/ship.bmp'. Make sure the destination is accurate. Mine was in a slightly different location as I have a project folder for the book.
Mine was 'Python Crash Course/Alien Invasion/images/ship.bmp'
I'm working through Think Python chapter 4, where they tell you to type the following to see if you have the turtle module:
import turtle
bob = turtle.Turtle()
This is supposed to open a new window, but instead, it crashes my computer. I've seen it freeze my computer where I can't move the cursor or change windows, and I have to restart it. And I've also seen it close all my programs and re-open them.
I'm running Python 3.7 downloaded from Anaconda on a Macbook Air with Mojave version 10.14.6.
EDIT: When I run BenajahTX's suggestion below, I get this error message printed 16 times:
CGSTrackingRegionSetIsEnabled returned CG error 268435459
Any clue what is causing this or how to fix it?
try and set the screen and mainloop
window = turtle.Screen()
window.setup(width,height)
while True:
window.update()
window.mainloop()
Try running the file in the terminal using the command:-
$ python filename.py
I had the same issue and I was VS code extension Code Runner to run the python file, however, when I tried the terminal it worked and the mac OS was not crashing this time
This question already has answers here:
how to play wav file in python?
(5 answers)
How can I play an mp3 with pygame?
(7 answers)
Closed 1 year ago.
import pygame
pygame.mixer.init()
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
When I run the code, there's no sound and the program ends in like a second. Initially I didn't have the while loop until I saw the suggestions in the answers to similar questions. The program does enter the while loop on my friend's windows system, but not on my mac, and it doesn't have any sound either even on my friend's windows system. Does anybody know how to solve it?
Works well on Ubuntu 10.04 and Pygame 1.9.1.
Some things you can try:
initialize whole pygame pygame.init()
i_4_got's suggestion (create a display) pygame.display.set_mode((200,100))
put a pause (tick) between play an get_busy
poll events inside the loop pygame.event.get()
Example:
import pygame
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
pygame.event.poll()
clock.tick(10)
import winsound
winsound.PlaySound(filename [as string with path if necessary], flag [integer z.B. 1 for asynchronous] )
This works fine with Windows 8 and Python 2.7.6 and *.wav files
The different flags you can check if you type winsound. and look for the autofill list.
The flags are like SND_FILENAME or similar.
If you type them into the editor like: *winsound.SND_FILENAME
you get the integer to apply in the PLaySound command from above
Have fun
Pygame sound not working fix
INFO
Try reinstalling pygame or upgrading it
I had version 1.9.1 which I upgraded to 1.9.3 by the following command
pip install pygame --upgrade
Note:
If you install it with easy_install it doesn't work . Not sure but
when I tried with easy_install it doesn't work
The code is the same as yours but i just replaced it with my music file to check it.
You can see it in the image :
In this image
.wav files seem to work well
I checked it with .mp3 files and found that it does'nt work.
You can convert .mp3 to .wav from : Converter
Or use PyMedia
If you do not want to convert it using any website.
You can also convert it with pydub.
You can download pydub from this
link
Or type this command in the command line:
pip install pydub
Usually, Pygame will not play mp3 files. You could test to see if .wav and .ogg files will play first, to make sure your code is correct (based on what you pasted, it seems to be right). I suggest converting your mp3 sounds to ogg for Pygame.
You have to add this to the end, in order for pygame to be happy:
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
Also, import sys.
it would be easier if u changed the mp3 to ogg format ,try this.. its simple
from pygame import *
pygame.init()
pygame.mixer.music.load("only one.ogg")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.3)
vote the answer if its working, if not tell me whats wrong