python实现bgm

from ctypes import windll, c_buffer
import random
def play(filename):
    w32mci = windll.winmm.mciSendStringA
    _alias = 'mp3_%s' % (str(random.random()))
    buffer = c_buffer(255)
    w32mci(str('open "%s" alias %s' % (filename,_alias)).encode(), buffer, 254, 0)
    w32mci(str('set %s time format milliseconds' % _alias).encode(), buffer, 254, 0)
    w32mci(str('status %s length' % _alias).encode(), buffer, 254, 0)
    len = int(buffer.value)
    start = 0
    end = len
    w32mci(str('play %s from %d to %d' % (_alias, start, end)).encode(), buffer, 254, 0)
    import time
    time.sleep(1000)
    w32mci(str('stop %s' % _alias).encode(), buffer, 254, 0)

注意filename的格式:
这里给出一个例子:filename = ‘C://Users//C-82//Music//a.mp3’
注:
这里的filename是你的本地mp3的绝对路径
程序默认播放时间1000s
欢迎关注公众号BBIT
让我们共同学习共同进步!

python实现bgm_第1张图片

你可能感兴趣的:(python实现bgm)