Home  >  Article  >  Backend Development  >  Detailed explanation of the error solution when loading mp3 files with Chinese names in pygame

Detailed explanation of the error solution when loading mp3 files with Chinese names in pygame

高洛峰
高洛峰Original
2017-03-31 10:04:343219browse

#pygame plays mp3 files:

Here the author only introduces an easy-to-understand method

Detailed explanation of the error solution when loading mp3 files with Chinese names in pygame

Open the official document, the author found, like discovery Like the New World, this guy is too powerful, and now I can only use it to play my mp3. Well, the rest will be discovered later. Whatever you say, here is a test code for playing mp3:

import pygame
path = 'Pam Pam - Noel Toto.mp3'pygame.mixer.init()
pygame.mixer.music.load(path)
pygame.mixer.music.play()

Among them, path represents the path and file name of the mp3 file to be played. Here the author uses the original idle, which can play the mp3 normally.

# Bug found:

Next This is the problem I encountered when I finished testing the music list in the afternoon, that is, when the name of the mp3 file to be loaded contains Chinese, the pygame.error.

Detailed explanation of the error solution when loading mp3 files with Chinese names in pygame

appears. It’s embarrassing. Through the output path, I found that there are no garbled Chinese characters. I googled and found that some people in the Chinese community said that python should try to avoid Chinese. Some people also said that the encoding can be changed. But the author tried, It seems impossible. English communities are similar to Stack Overflow, but there is no solution to this problem. After all, foreigners don't care much about "our own affairs". So, after crawling for a long time, I couldn't find any effective solution. Solution. Well, this is a flaw. Of course, experts should be able to solve it by changing the source code in pygame. This is not something I can do^_^.

#debug:

The author came up with a fun method, and there should be a better implementation method. Let me first shamelessly talk about my debugging ideas:

First of all, when encountering the Chinese name mp3, pygame appears. .error At this time, a serious error occurred in the program. It is natural to think of the try statement. In this way, when an error occurs when the Chinese name mp3 is encountered, it will automatically go to the except content. The author thought of copying the file to be played to the specified directory, And rename it to non-Chinese. Finally, load this non-Chinese mp3 file, thereby bypassing the flaw in pygame's Chinese recognition. The code below:

import pygameimport shutil
path = '一走过的日子-刘德华.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()

# Running effect:

Detailed explanation of the error solution when loading mp3 files with Chinese names in pygame

At this point, the program can play Chinese mp3 files normally. In fact, this problem does not essentially solve the Chinese recognition problem of pygame, but bypasses this problem. This method also has a drawback, that is, it will cause The file play.mp3 can be placed in the cache directory when working on a project.

At this point, the description of solving the pygame Chinese recognition problem encountered this afternoon is completed. If there is any better Please comment and let me know the method, thank you.

I just looked at the python3.3.3 pygame directory and did some science on the file mixer.pyd. I found that it has the .pyd suffix and is a binary file like dll. To change the content, you need to decompile it. .In other words, python is not completely open source.

The above is the detailed content of Detailed explanation of the error solution when loading mp3 files with Chinese names in pygame. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn