0

When you try to use pygame.mixer.music.open() with a filename string containing Unicode characters, it seems to throw a UnicodeEncodeError all the time:

File "C:\TestPlayer.py", line 43, in <module>
pygame.mixer.music.load(x)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 12-19: 
ordinal not in range(128)

(lines broken for your viewing pleasure)

I checked for x's existance using os.path.exists(x), which returned True. Am I doing something wrong? If not, is it possible to manually fix pygame's mixer (which is a .pyd file)?

I'm using Python 2.6, and Pygame 1.9.1.

I forgot to add the file I tried opening is an mp3 file, but Pygame's website/wiki states pygame.mixer.music should work with those. In fact, it does, as long as the filename only contains ASCII characters.

lvk
  • 75
  • 6

2 Answers2

0

Instead of passing a filename, open the file in a unicode compatible way and pass the file object to pygame.mixer.music.load

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
0

You tried

fle = open(filename, 'rb')
pygame.mixer.music.load(fle)

and

fle = open(filename, 'rb')
pygame.mixer.load(fle.read())

Or you could try, i dont know, something like

fle = open(filename, 'rb')
foo = fle.read()
pygame.mixer.load(fle.encode('ascii'))
Fabio
  • 69
  • 6
  • FYI, from PEP8: If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus "print_" is better than "prnt". (Perhaps better is to avoid such clashes by using a synonym.) – Mu Mind Feb 27 '12 at 07:16