如何使用 pygame 播放 mp3? [英] How can I play an mp3 with pygame?

查看:74
本文介绍了如何使用 pygame 播放 mp3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import pygame
file = 'some.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()

这会输出进程已完成,退出代码为 0",但它不播放任何内容.我该如何解决这个问题?

This outputs, "Process finished with exit code 0", but it doesn't play anything. How can I resolve this problem?

推荐答案

play 函数开始播放音乐,但立即返回.然后你的程序结束,pygame 对象会自动销毁,导致音乐停止.

The play function starts the music playing, but returns immediately. Then your program reaches it's end, and the pygame object is automatically destroyed which causes the music to stop.

正如您所评论的,如果您在退出之前等待它,它确实会播放音乐 - 因为这样 pygame 对象在 while 循环完成之前不会被销毁.

As you commented, it does play the music if you wait for it before exiting - because then the pygame object isn't destroyed until the while loop finishes.

while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)

这篇关于如何使用 pygame 播放 mp3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆