Detailed pygame loading Chinese name MP3 File Error Resolution method

Source: Internet
Author: User
#pygame播放mp3文件:

Here I only introduce a good way to understand

Opening the official document, the author found that, like the discovery of the new world, this guy is too strong, and now I can only use it to play my mp3. Well, the rest of the wait to discover it later. What to say, to play the MP3 test code:

Import Pygamepath = ' Pam pam-noel Toto.mp3 ' Pygame.mixer.init () pygame.mixer.music.load (path) Pygame.mixer.music.play ( )

Where path represents the path and file name of the MP3 file to be played. Here the author uses the source of the idle, can play this mp3 normal.

#发现Bug:

Then is the author in the afternoon to finish the music list test play when the problem encountered, that is, when the MP3 file name to be loaded contains Chinese, the pygame.error appears.

This is embarrassing, through the output path, found that the Chinese is not garbled, the author of Google, found that the community of Chinese people say python should try to avoid Chinese. Some people say that you can change the code. But I tried, it seems not possible. English community similar to stack Overflow, There are no solutions to this problem. After all, foreigners don't care too much about ' our own business '. So, climbing for a long while, did not find effective solutions. Well, this is a mishap. Of course, Daniel should be able to change the source code in Pygame, this is not what I can do ^_^.

#debug:

The author has come to a fun way, there should be a better way to achieve, I first come to the thick cheek to say my debug ideas:

First, when the Chinese name MP3 is encountered Pygame.error at this time, the program has a serious error. It is natural to think of a try statement. This way, when you encounter the Chinese name mp3 error, automatically go to the except content. The author thought of copying the file to be played to the specified directory, Rename to non-Chinese. Finally loading this non-Chinese MP3 file, bypassing the Pygame Chinese recognition. The following code:

Import Pygameimport Shutilpath = ' A passing day-Andy Lau. mp3 ' Pygame.mixer.init () Try:    pygame.mixer.music.load (path)    Pygame.mixer.music.play () except:    print (' Something is wrong~\nbut I see you. ')    Shutil.copyfile (Path, ' Play.mp3 ')    pygame.mixer.music.load (' Play.mp3 ')    Pygame.mixer.music.play ()

#运行效果:

At this point, the program has been able to play the Chinese name mp3 file. In fact, this problem does not solve the problem of pygame Chinese recognition in essence, but bypasses the problem. This method also has a disadvantage, that is, will produce Play.mp3 this file, in the project can be placed in the cache directory.

At this point, about the resolution of this afternoon encountered Pygame Chinese recognition problem narrative finished, if there is a better way, hope please comment to inform, thank you.

Just looked at the next python3.3.3 pygame directory, file Mixer.pyd Popular science a bit. PYD suffix, found to be the same as the DLL is a binary file, the changes need to decompile. That is, Python is not completely open source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.