问题描述
我正在开发一个tkinter应用程序,其中视频和声音根据某些事件排队并播放。
为此,我使用python-vlc,但是我没有找到一种同时播放多种声音的方法(除了多线程处理之外)。这些视频还需要有自己的窗口才能工作。
以下代码来自纯用于对此进行测试的模块。
import vlc
files = ['/home/silver/Desktop/hl1sfx/sound/gman/gman_potential.wav','/home/silver/Desktop/hl1sfx/sound/gman/gman_nasty.wav','/home/silver/Desktop/hl1sfx/sound/gman/gman_Nowork.wav','/home/silver/Downloads/atlas_motor_jitter.mp4']
instance = vlc.Instance ()
medias = [instance.media_new (f) for f in files]
player = vlc.MediaPlayer ()
for m in medias:
input ('>> ')
player.set_media (m)
player.play ()
if player.is_playing ():
p = vlc.MediaPlayer (m)
p.play ()
input ('next?')
即使创建新的媒体播放器也不起作用。是为每个文件使用单独的线程解决方案,还是我忽略了python-vlc中的某些功能?
解决方法
可以,但是需要单独的实例。
这是使用列表进行操作的一种简单而又参差不齐的方法。我相信您可以随着时间的推移设计出一种更清洁的方法。
import vlc
import time
files = ['./V2.mp4','./vp1.mp3','./V3.mp4']
instances = []
medias = []
players = []
for idx,fname in enumerate(files):
print("Loading",fname)
instances.append(vlc.Instance())
medias.append(instances[idx].media_new(fname))
players.append(vlc.MediaPlayer())
players[idx].set_media(medias[idx])
players[idx].play()
player_count = players # copy of the players list so we don't modify during iteration
still_playing = True
time.sleep(0.5) # Wait for players to start
while still_playing:
time.sleep(1)
for p in players:
if p.is_playing():
continue
else:
player_count.remove(p)
players = player_count # no point iterating over players that have finished
print("Finished - Still playing ",str(len(player_count)))
if len(player_count) != 0:
continue
else:
still_playing = False
p.s。给主持人,他删除了我对这个问题的另一个答案的质量的评论。您是否同意“阅读手册”类型的答案或评论?你太无耻了。以这种态度,这个站点将去狗。
我等待您的surreptitious
编辑。
使用mpyg321进行此操作:
pip3 install mpyg321
from mpyg321.mpyg321 import MPyg321Player
然后:
while True:
player = MPyg321Player()
player.play_song(input('Path To Song'))
谢谢
,如果您想使用python-vlc
您可以使用“线程”在后台播放音乐或...。
import threading
def MusicPlay(Name):
#Your Code
while True:
threading.Thread(target=Delete,args=('NameOfMusic',)).start()