Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

audio - 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found this problem really interesting, so I downloaded the vlc python module and tinkered with it, and I think I found a solution for you.

What I did is I built an array containing the VLC instances, I built a function that creates VLC instances, and a loop that makes sure they're running (and produces verbose output).

This does require you make sure that your VLC player allows multiple window instances.

My code:

import vlc
import time

VLCObjects = []

def VLCInstance(src, volume):
    
    # creating vlc media player object 
    try:
        vlc_instance = vlc.Instance() 
        player = vlc_instance.media_player_new() 
        media = vlc_instance.media_new(src)
        player.set_media(media)
    except:
        return("Error: Was unable to mount media")
    else:
        pass
    
    VLCObjects.append(player)

    VLCObjects[0].audio_set_volume(volume) 
    VLCObjects[0].play() 

    # give time to initialize, then get length of audio & wait until done playing
    time.sleep(1) 
    duration = VLCObjects[0].get_length() 
    #time.sleep(duration)
    return("Success: Song playing")

Then you'd make all your calls or whatever here, and then the loop:

song1 = ...
song2 = ...
VLCInstance(song1, 100)
VLCInstance(song2, 20)
while(True):

    playing = False
    
    time.sleep(1) 
    
    print(VLCObjects)
    for x in range(0, len(VLCObjects) ):
        value = VLCObjects[x].is_playing()

        if value == True:
            print("... VLCObject #%d is playing" % x)
            playing = True
            
        else:
            print("... VLCObject #%d needed to be restarted" % x)
            VLCObjects[x].play()
            
    if playing == False:
        break

The resulting output will look something like this, and there will be two non-windowed VLC players.

Success: Song playing
Success: Song playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
0
0
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing
[<vlc.MediaPlayer object at 0x7fa56d271520>, <vlc.MediaPlayer object at 0x7fa56d271cd0>]
... VLCObject #0 is playing
... VLCObject #1 is playing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...