1

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

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100

1 Answers1

0

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.

import random
  • 3,054
  • 1
  • 17
  • 22