Cannot play ogg file in Python - python

So I tried to convert my midi file to ogg and I still cant get it to work... hope some of you can help me?
This is my code:
showTextScreen('Tetromino')
while True: # game loop
if random.randint(0, 1) == 0:
pygame.mixer.music.load('tetrisb.mid')
else:
pygame.mixer.music.load('tetrisc.mid')
pygame.mixer.music.play(-1, 0,0)
runGame()
pygame.mixer.music.stop()
showTextScreen('Game Over')
It gives the error that it cant open tetrisb.mid
I tried renaming it and it still says the same...
If i can fix this I can fix the other file probably
Here is the error:
Traceback (most recent call last):
File "H:\Programmering\Python\TETRIS!.py", line 487, in <module>
main()
File "H:\Programmering\Python\TETRIS!.py", line 166, in main
pygame.mixer.music.load('tetrisc.mid')
pygame.error: Couldn't open 'tetrisc.mid'
I'm using windows and python 3.8 and i dont mind if im using midi or ogg...
I changed my midi file into ogg, hoping it would fix the problem but it didnt.
My file is ogg atm

It looks like you need to supply the correct path to your midi file. This question about how to check for file existence might also be helpful.
The error messages from pygame.mixer.music.load are different if the file format is not supported or non-existent:
>>> pygame.mixer.music.load("nonexistent.file")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Couldn't open 'nonexistent.file'
>>> pygame.mixer.music.load(r"c:\tmp\empty.file")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Couldn't read from RWops
>>> pygame.mixer.music.load(r"c:\tmp\robots.txt")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Module format not recognized
>>> pygame.mixer.music.load(r"C:\Windows\media\flourish.mid")
>>> pygame.mixer.music.play(-1, 0)
>>> pygame.mixer.music.stop()
Note that when specifying a file path a \ is an escape character which is processed differently. So you can use \\ instead, or put an r at the start of the string, before the quotation mark as I've shown above.

Related

Pgzero errors that I cant figure out

I am writing a python (pgzero) project for a simple game with my teacher, but I wanted to improve it a bit. After adding something like a minimap, everything was fine, but after writing the next stage of the code, it stopped working completely. For other students who prescribed code identically to the teacher, everything works properly, unfortunately for me, it does not. Thank you in advance for any help. If more project code is needed I can share.
Terminal error:
Traceback (most recent call last):
File "C:\Users\Olek\AppData\Local\Programs\Python\Python38\lib\site-packages\pgzero\clock.py", line 168, in tick
cb()
File "C:/Users/Olek/PycharmProjects/gra_mars_alfa/gra.py", line 533, in petla_glowna
if mapa_pom[bohater_y][bohater_x] not in ob_mozna_stac:
NameError: name 'mapa_pom' is not defined
If I'm using terminal to run project terminal says:
PS C:\Users\Olek\PycharmProjects\gra_mars_alfa> pgzrun gra.py
pygame 2.1.0 (SDL 2.0.16, Python 3.8.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "c:\users\olek\appdata\local\programs\python\python38\lib\runpy.py", line 192, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\olek\appdata\local\programs\python\python38\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Olek\AppData\Local\Programs\Python\Python38\Scripts\pgzrun.exe\__main__.py", line 7, in <module>
File "c:\users\olek\appdata\local\programs\python\python38\lib\site-packages\pgzero\runner.py", line 77, in main
src = f.read()
File "c:\users\olek\appdata\local\programs\python\python38\lib\encodings\cp1250.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 24633: character maps to <undefined>
I says that the class: mapa_pom is not defined.
Meaning that mapa_pom[][] is not what you wanted to use
your error is:
NameError: name 'mapa_pom' is not defined
your code is:
if mapa_pom[bohater_y][bohater_x] not in ob_mozna_stac:
so you are saying: if hero.x hero.y is not in list where_you_can_stand:
'code here'
if map[hero.x][hero.y] not in where_you_can_stand:
'code here'
variable error example:
print(i)
Traceback (most recent call last):
File "/home/pi/mu_code/i.py", line 1, in
print(i)
NameError: name 'i' is not defined
an_actor = Actor('not_here', anchor=('center', 'middle'))
Traceback (most recent call last):
File "/home/pi/mu_code/i.py", line 1, in
an_actor = Actor('not_here', anchor=('center', 'middle'))
NameError: name 'Actor' is not defined
If this doesn't work please show me where in the code where you use mapa_pom and I might be able to answer it better.

I am having trouble reading RData using pyreadr

I have installed pyreadr and would like to use it to read .RData. I have followed the direction given from the previous comment on here Loading RData but that did not work for me.
When I try the following line (with the path pointing to the right file):
result = pyreadr.read_r('/path/to/file.RData')
I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/apps/bioapps/python/Python-3.6.1/lib/python3.6/site-packages/pyreadr/pyreadr.py",
line 80, in list_objects
parser.parse(path)
File "pyreadr/librdata.pyx", line 117, in pyreadr.librdata.Parser.parse
File "pyreadr/librdata.pyx", line 142, in pyreadr.librdata.Parser.parse
pyreadr.custom_errors.LibrdataError: Unable to read from file
Please let me know if you have any suggestions.
Thanks!

How to pretty print or format stack trace from python?

I receive python stack traces from logs and they sometimes come in very weird format, e.g. no new lines or incorrectly formated. Here is an syntactic example:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in bar File "<stdin>", line 2, in foo ValueError
Unfortunately, there is no way I can change the format of the logs, as I don't have an access to that system.
Is there an automatic way of formatting those exceptions to standard python format, so that it would look something like this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
File "<stdin>", line 2, in foo
ValueError
I've quickly went through traceback module's documentation, but it seems it's only for runtime exceptions.

Errors when connecting to nxt brick

I'm using nxt-python to connect to my nxt brick from my raspberry pi.
After some struggle with the python 3.2 versions of the bluetooth and usb libraries it requires i've managed to get it to connect via bluetooth and ask for a passkey. After some more struggle i found that i could type bluetooth-agent PASSKEY & (and replace PASSKEY with the passkey) in the terminal before running the python script that connects with the nxt brick and as far as i know that works fine. But now it throws an error. Connecting with USB throws a different error.
Here's the error i get with both methods (USB & BT) and debug enabled:
USB: True BT: True Fantom: False FUSB: False FBT: False
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/nxt/locator.py", line 138, in find_one_brick
if name and info[0].strip('\0') != name:
TypeError: Type str doesn't support the buffer API
Failed to connect to possible brick
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/nxt/locator.py", line 131, in find_one_brick
info = b.get_device_info()
File "/usr/local/lib/python3.2/dist-packages/nxt/brick.py", line 27, in poll
self.sock.send(ogram.bytes())
File "/usr/local/lib/python3.2/dist-packages/nxt/bluesock.py", line 57, in send
l0 = len(data.encode('utf-8')) & 0xFF
AttributeError: 'bytes' object has no attribute 'encode'
Failed to connect to possible brick
No brick was found.
Is the brick turned on?
For more diagnosing use the debug=True argument or
try the 'nxt_test' script located in /bin or ~/.local/bin
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/dist-packages/nxt/locator.py", line 154, in find_one_brick
raise BrickNotFoundError
nxt.locator.BrickNotFoundError
Because i know python i could probably fix the errors by just editing the source code, but i'm affraid i'll break something. I've tried editing it (keeping backups of every file i edit) and then it gets past a couple of lines before throwing another different error.
EDIT
When i try to import using python 2 it gives me this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/nxt/__init__.py", line 15, in <module>
from nxt.locator import find_one_brick, Method
File "/usr/local/lib/python2.7/dist-packages/nxt/locator.py", line 49
if not silent: print("USB module unavailable, not searching there", file=sys.stderr)
^
SyntaxError: invalid syntax
I've downloaded the v2.2.2 stable instead of cloning the master commit and it runs fine under python2. Too bad it isn't python3 though.

when does bz2.decompress(rawdata) got an error of "couldn't find end of stream"?

I have checked that the file in my disk is the same as https://github.com/scipy/scipy/blob/master/scipy/misc/face.dat
but when I run this statement ,I have got this error. why ?
scipy.misc.face()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\scipy\misc\common.py", line 567, in face
data = bz2.decompress(rawdata)
ValueError: couldn't find end of stream
It's a bug (on Windows), see here
https://github.com/scipy/scipy/commit/7f0210fd28a2b3be79806d5cae462be52c1601e9

Categories

Resources