在python上播放mp3歌曲 [英] Playing mp3 song on python
问题描述
我想从 python 播放我的歌曲 (mp3),你能给我一个最简单的命令吗?
I want to play my song (mp3) from python, can you give me a simplest command to do that?
这是不正确的:
import wave
w = wave.open("e:/LOCAL/Betrayer/Metalik Klinik1-Anak Sekolah.mp3","r")
推荐答案
试试这个.这很简单,但可能不是最好的方法.
Try this. It's simplistic, but probably not the best method.
from pygame import mixer # Load the popular external library
mixer.init()
mixer.music.load('e:/LOCAL/Betrayer/Metalik Klinik1-Anak Sekolah.mp3')
mixer.music.play()
请注意,pygame 的对MP3
的支持是有限的.此外,正如 Samy Bencherif 所指出的那样,不会有运行上述代码时会弹出任何愚蠢的 pygame 窗口.
Please note that pygame's support for MP3
is limited. Also, as pointed out by Samy Bencherif, there won't be any silly pygame window popup when you run the above code.
安装很简单 -
$pip install pygame
更新:
以上代码只会在交互式运行时播放音乐,因为 play()
调用将立即执行并且脚本将退出.为避免这种情况,您可以改为使用以下代码等待音乐播放完毕,然后在将代码作为脚本运行时退出程序.
Above code will only play the music if ran interactively, since the play()
call will execute instantaneously and the script will exit. To avoid this, you could instead use the following to wait for the music to finish playing and then exit the program, when running the code as a script.
import time
from pygame import mixer
mixer.init()
mixer.music.load("/file/path/mymusic.ogg")
mixer.music.play()
while mixer.music.get_busy(): # wait for music to finish playing
time.sleep(1)
这篇关于在python上播放mp3歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!