An error or pygameerror occurs when pygame loads an mp3 file with a Chinese name.
I haven't written anything for a while. I 've been working on a Python pygame-based music player in the last few days. I wanted to finish it and try again to write an article, but the deeper it gets, the framework is coming out, it may be due to my personal problems and slow time. let's talk about pygame. mixer. music. load () Loading mp3 files with Chinese names.
(Posting time 22:45:58 original link http://www.cnblogs.com/dearvee/p/6649055.html)
# Preface:
I just made a music list today, but I found that when the mp3 file name in the specified directory is Chinese, pygame appears. error. The specified mp3 file cannot be correctly loaded. I don't want to change the written code any more. Let's test it.
# Pygame play mp3 files:
Here I will only introduce a good understanding method. For details, refer to the official pygame. mixer. music documentation.
Open the official document and I found that, like discovering the New World, this guy is so powerful, but now I can only use it to play my mp3. well, let's explore it later. what do you mean? Play the mp3 test code:
import pygamepath = 'Pam Pam - Noel Toto.mp3'pygame.mixer.init()pygame.mixer.music.load(path)pygame.mixer.music.play()
Here, path indicates the path and file name of the mp3 file to be played. Here I use the source idle and can play this mp3.
# Bugs discovered:
Next, I encountered a problem when I completed the music list test playing in the afternoon, that is, when the mp3 file name to be loaded contains Chinese characters, the pygame. error.
This is embarrassing. Through the output path, I found that Chinese characters are not garbled. I Googled and found that some people in the Chinese community said that python should avoid Chinese characters as much as possible. some people say they can change the encoding. however, I tried it and it seems that I cannot. the English community is similar to Stack Overflow, and there is no solution to this problem. after all, foreigners do not care much about ''our own things ''. therefore, after a long time, no effective solution was found. okay, this is a serious injury. of course, the experts should be able to solve the problem by changing the source code in pygame. This is not what I can do ^_^.
# Debug:
I have come up with a fun method and there should be better implementation methods. First, I will give a thick face to my debug ideas:
First, when the Chinese name mp3 is used, pygame appears. at this time, the program encountered a serious error. it is natural to think of the try statement. in this way, when an error occurs in the Chinese mp3 name, it is automatically transferred to the audio t content. I want to copy the file to the specified directory and rename it as non-Chinese. finally, this non-Chinese mp3 file is loaded, bypassing the Chinese recognition of pygame. the code below:
Import pygameimport shutilpath = 'A passing Sunday -liu dehuawei' pygame. mixer. init () try: pygame. mixer. music. load (path) pygame. mixer. music. play () failed T: print ('Something is wrong ~ \ Nbut I see you. ') shutil.copyfile(path,'playout') pygame.mixer.music.load('playout') pygame. mixer. music. play ()
# Running effect:
This file can be stored in the cache directory during project creation.
So far, the description of the pygame Chinese recognition problem solved this afternoon has been completed. If there is a better solution, Please comment and let us know. Thank you.
Just now I read the python3.3.3 pygame directory, and the file mixer. pyd is popular. pyd suffix. It is found that the file is a binary file like dll, and the change content needs to be decompiled. That is to say, python is not completely open-source.