gpt4 book ai didi

python - 如何在Python中依次播放音频文件的序列?

转载 作者:行者123 更新时间:2023-12-03 01:32:43 25 4
gpt4 key购买 nike

我想在Python 3.7.3中制作一个音频播放器,该音频播放器将依次播放歌曲。
在这里,您将看到功能“JustPlay”。只播放第一首歌曲,而不是一个接一个地播放。之前没有“休息”,只播放了最后一个。如何使其依次播放?

from tkinter import *
from pygame import mixer
import random

root = Tk()

menubar = Menu(root)
root.config(menu=menubar)

subMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File",menu=subMenu)
subMenu.add_command(label="Open")
subMenu.add_command(label="Exit")

subMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help",menu=subMenu)
subMenu.add_command(label="About us")

mixer.init()
root.title("Melody")
text = Label(root, text = "The name of an album")
text.pack()

a = "C:/Pathway1"
b = "C:/Pathway2"
c = "C:/Pathway3"
d = [a, b, c]

def song1():
mixer.music.load(a)
mixer.music.play()

def song2():
mixer.music.load(b)
mixer.music.play()

def song3():
mixer.music.load(c)
mixer.music.play()

def stop_music():
mixer.music.stop()

def set_vol(val):
volume = int(val) / 100
mixer.music.set_volume(volume)

def Randomplay():
mixer.music.load(random.choice(d))
mixer.music.play()

def JustPlay():
for x in d:
mixer.music.load(x)
mixer.music.play()
break


playbtn1 = Button(root, text="song1", command=song1)
playbtn1.pack()

playbtn2 = Button(root, text="song2", command=song2)
playbtn2.pack()

playbtn3 = Button(root, text="song3", command=song3)
playbtn3.pack()

randombtn = Button(root, text="Random", command=Randomplay)
randombtn.pack()

justplaybtn = Button(root, text="Play", command=JustPlay)
justplaybtn.pack()

stopbtn = Button(root, text="Stop", command=stop_music)
stopbtn.pack()

scale = Scale(root, from_=0, to=100, orient=HORIZONTAL, command=set_vol)
scale.pack()

root.mainloop()

最佳答案

您可以在Mixer.music中使用.queue()函数将一堆歌曲排队-因此,我将加载一首歌曲,将其余歌曲排队,然后运行play()

mixer.music.load(a)
[mixer.music.queue(track) for track in [b,c,d]]
mixer.music.play()

关于python - 如何在Python中依次播放音频文件的序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56251484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com