This article mainly explains how to solve the error when pygame loads mp3 files of Chinese names. it has some reference value. if you are interested, refer to # pygame to play mp3 files:
Here I will only introduce a good understanding method.
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.
The above is a detailed explanation of the solution for an error in the mp3 file loaded by pygame. For more information, see other related articles in the first PHP community!