Trouble playing an MP3 file in python via Pyglet - python

Summary: (Trying to play MP3 in python via pyglet library, AVbin not detected, Mp3 not played)
So i'm trying to to play an MP3 file in python with pyglet. i installed pyglet with the command prompt and all that good stuff, and I went to the AVbin website and downloaded and ran their .exe file. Im not sure if just running the .exe file actually installs AVbin, but that's all ive done with it so far. So my problem is im trying to play an mp3 that i have located on my desktop, but Pyglet still doesnt recognize AVbin? Here's the code as well as the error message:
Code:
import pyglet
music = pyglet.media.load(r"C:\Users\Doug\Desktop\01 Gem Shards - 4A.mp3")
music.play()
pyglet.app.run()
Error:
Traceback (most recent call last):
File "C:/Python34/test.py", line 3, in <module>
music = pyglet.media.load(r"C:\Users\Doug\Desktop\01 Gem Shards - 4A.mp3")
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\__init__.py", line 1429, in load
source = get_source_loader().load(filename, file)
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\__init__.py", line 1410, in load
return riff.WaveSource(filename, file)
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\riff.py", line 201, in __init__
'AVbin is required to decode compressed media')
pyglet.media.riff.WAVEFormatException: AVbin is required to decode compressed media

Add AvBin to your PATH enviroment variable:
you can do this with the folowwing command
setx path "%path%;c:\pathToAvBin"
plase note that you need to change "c:\pathToAvBin" for your real path to av bin :)
BE CAREFULL TO NOT CLEAR YOUR PATH

Related

File not found in working directory pygame

I am attempting to make a simple pygame game, and I need to load in some images. When I run my code, or:
bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
It gives me an error which says:
Traceback (most recent call last):
File "C:\Users\victo\Desktop\Assets - Pulga\mainpulga.py", line 12, in <module>
bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
FileNotFoundError: No file 'assets/Fondotiff.tif(1).png' found in working directory 'C:\Users\victo\Desktop\Assets - Pulga'.
***Repl Closed***
I have double checked if the image is inside the folder on my desktop and it is there, but for some reason it doesn't find it.
How can I fix this?
The asset file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
e.g.:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

pyinstaller not using qtmodern data

When using the qtmoden library with python.
It works fine when running the code in VS Code.
But after using pyinstaller, it doens't anymore. When opening teh generated .exe file, it states that it does not have access to files located in - C:\Users\MyUsername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\qtmodern\resources
When running the .exe aplication it errors stating:
Traceback (most recent call last):
File "my_filename.py", line 262, in (this number differs depending on my app/code)
File "qtmodern\styles.py", line 70, in dark
File "qtmodern\styles.py", line 23, in _apply_base_theme
FileNotFoundError: [Errno 2] No such file or directory: " 'C:\sers\MyName\AppData\Local\Temp_MEI166802\qtmodern\resources/styles.qss'
[36020] failed to execute script my_filename
Anybody any idea how to get this fixed?
How to make sure pyinstaller also takes these 2 stypes.py files into account?
I tried adding data with pyinstaller via the --add--data command and including paths with --paths command, But the error message stays the exact same.

Failed to play an audio file with Python's playsound

I tried to use the playsound module to play an audio file. When I run my code I get this error:
Traceback (most recent call last):
File "/Users/Tarantino/Desktop/CodeTesting/sound.py", line 3, in <module>
playsound('sound.mp3')
File "/Users/Tarantino/Library/Python/3.8/lib/python/site-packages/playsound.py", line 67, in _playsoundOSX
raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/Tarantino/Desktop/Code Testing/sound.mp3
I have the audio file in the same folder as the Python code, could I please get help on what to do?
Move file you're playing to folder that has no special or non-ascii characters on the full path from root of disk, spaces in files/dirs names are not allowed too. Because playsound is creating an URL from full path-location of file and this URL should has valid for URLs chars.
E.g. /x/y/z.mp3 path is alright, but /x y/z.mp3 and /x[y]/z.mp3 and /x/漢/z.mp3 are not.
Also try upgrading library via python -m pip install --upgrade playsound, I've just installed library and tried playing sound and it plays for me.
Also some dir on the path might not have read permissions for the user you are running python with.

Error with fonts when using pyinstaller to convert a pygame app to executable

I created a pygame app that has one main python file and an assets folder. I have used pyinstaller to convert the app to an executable file, but every time I receive errors - when I resolve them, different errors come up. I suspect the errors I have been getting recently have to do with the text in my pygame file.
This is the pygame font line I used:
font = pygame.font.SysFont("Arial", 32)
I have tried using .ttf files and copying them into the same folder as my main.py file, but that didn't work either. I also make sure to copy my assets folder into the same folder as the main.app file, but that has not helped either.
I am not sure what this error message is telling me, and there seems to be no other question with this same message (there is no line in my code that is referenced in this error message for me to check).
Fatal Python error: (pygame parachute) Segmentation Fault
Traceback (most recent call last):
File "pygame/pkgdata.py", line 50, in getResource
File "site-packages/pkg_resources/__init__.py", line 1134, in resource_exists
File "site-packages/pkg_resources/__init__.py", line 1404, in has_resource
File "site-packages/pkg_resources/__init__.py", line 1473, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
Any help would be amazing, thank you!
If you use ttf-files, then you need to import them directly instead of using SysFont. So as an example:
size = 12
myfont = pygame.font.Font("Arial.ttf",size)
Arial.ttf also needs to be placed in the same folder where your executable is, or if you have a deeper folder structure, use relative paths (and the folders need to be next to your executable as well):
size = 12
myfont = pygame.font.Font("./myfolder/Arial.ttf",size)
For testing purposes it might also help to use the following command to see from which folder you start, to set your relative path and to know where you are looking for the ttf file:
import os
os.getcwd()

Can open a program in IDLE but not in Terminal,Atom etc

Hi I'm new to python and pygame but I am using Atom but I can't solve this problem. When I open my script in IDLE it works but I can't make it work on Atom (specifically terminal-plus) and Terminal. My error message is
Traceback (most recent call last):
File "cargame.py", line 32, in <module>
carImg = pygame.image.load('racecar.png')
pygame.error: Failed loading libpng.dylib: dlopen(libpng.dylib, 2): image not found
so can anyone help?
Perhaps the reason why pygame can't load the image is because it has no context of where the module is being loaded from. Make sure the module (essentially the file where your source code exists), and image file exist in the same environment on disk (the same folder). Otherwise pass an absolute path to the load function and see if the image can be located.

Categories

Resources