pythonlibvlcpython-vlc

playing multiple audio / video files simultaneously with python-vlc


I'm developing a tkinter application where video's and sounds are queued and played back based on certain events.

For this I use python-vlc, but I haven't found a way to play multiple sounds at the same time (besides multithreading). The videos would also need their own window for this to work.

the following code is from a module pure for testing this.

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?')

Even creating a new mediaplayer doesn't work. Is seperate threads for each file the solution or am I overlooking some feature in python-vlc?


Solution

  • You can but you'll need separate instances.
    Here is a simple and rather ragged way of doing it using lists. I'm sure you can devise a cleaner method, with time.

    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. to the moderator who removed my comment about the quality of another answer to this question. Do you condone "read the manual" type answers or comments? Shame on you. With that attitude this site will go to the dogs. I await your surreptitious edit.