Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Create Multiple Players with Python VLC and Have Different Volumes on Them?

I am trying to mix two .mp3 songs together by simply changing the volumes of two songs, similar to DJing. However, when I set the volume of a player, both players' volumes are being changed to the value that I've last set. I would like to create two separate players that have different volume properties AKA for example to have one player with volume(100) and the other set to volume(20). Here is how I am doing it:

import vlc
import time

# Path for mp3 file
song = 'C:/Users/Admin/Desktop/Projects/Music Shit/Martin Garrix - Animals (Original Mix).mp3'

# Set up and play player with volume 100
player = vlc.MediaPlayer(song)
media = vlc.Media(song)
player.set_media(media)
player.audio_set_volume(100)
player.play()

# Path for second mp3 file
song2 = 'C:/Users/Admin/Desktop/Projects/Music Shit/Tremor (Sensation 2014 Anthem).mp3'

# Set up and play second player with volume 20
player2 = vlc.MediaPlayer(song2)
media2 = vlc.Media(song2)
player2.set_media(media2)
player2.audio_set_volume(20)
player2.play()

When I run this, both songs play at volume 20, which is undesired. I believe that they are linked to one player, which I do not want. I'd like to have two separate players with different volumes.

By the way when I tried this on Mac it worked, and the audio had different volumes, but I am currently on Windows and it is not working. Seems weird!

Any help would be much appreciated. It's my first time submitting a question!

like image 812
yellowazns123 Avatar asked Oct 27 '25 15:10

yellowazns123


2 Answers

A variation on a theme, using multiple instances of vlc.Instance is the way to go.

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))
    time.sleep(1) # used solely to gauge the difference in volumes
    players.append(vlc.MediaPlayer())
    players[idx].set_media(medias[idx])
    players[idx].play() 

players[1].audio_set_volume(120)
players[2].audio_set_volume(80)
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
        
like image 161
Rolf of Saxony Avatar answered Oct 30 '25 05:10

Rolf of Saxony


vlc.Instance() doesn't work on win11 and vlc-3.0.20 for me.

It seems that MMDevice causes this problem.
VLC 3.X uses MMDevice by default, instead of DirectSound which 2.X version uses.

vlc.Instance("--aout=directsound")

This works in my case.

Reference: https://forum.videolan.org/viewtopic.php?t=147229

like image 38
Kaede Avatar answered Oct 30 '25 06:10

Kaede



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!