使用 python-vlc 同时播放多个音频/视频文件

playing multiple audio / video files simultaneously with python-vlc

我正在开发一个 tkinter 应用程序,其中视频和声音根据特定事件排队和播放。

为此,我使用 python-vlc,但我还没有找到同时播放多个声音的方法(多线程除外)。这些视频还需要它们自己的 window 才能正常工作。

以下代码来自一个纯粹用于测试的模块。

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 中的某些功能?

用 mpyg321 做这个:

pip3 install mpyg321
from mpyg321.mpyg321 import MPyg321Player

然后:

while True:
    player = MPyg321Player()
    player.play_song(input('Path To Song'))

谢谢

如果你想使用python-vlc

您可以使用 'Thread' 并在后台播放音乐或...。

import threading

def MusicPlay(Name):
    #Your Code

while True:
    threading.Thread(target=Delete, args=('NameOfMusic',)).start() 

可以,但需要单独的实例。
这是使用列表进行操作的一种简单而简陋的方法。我相信随着时间的推移,您可以设计出更简洁的方法。

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 编辑。