1

我试图通过简单地更改两首歌曲的音量来将两首 .mp3 歌曲混合在一起,类似于 DJ。但是,当我设置播放器的音量时,两个播放器的音量都被更改为我上次设置的值。我想创建两个具有不同音量属性 AKA 的单独播放器,例如让一个播放器的音量为(100),另一个设置为音量(20)。这是我的做法:

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()

当我运行它时,两首歌曲都以 20 音量播放,这是不受欢迎的。我相信他们与一名球员有关,这是我不想要的。我想要两个不同音量的独立播放器。

顺便说一句,当我在 Mac 上尝试它时它可以工作,并且音频有不同的音量,但我目前在 Windows 上并且它不工作。好像很奇怪!

任何帮助将非常感激。这是我第一次提交问题!

4

2 回答 2

0

我发现这个问题真的很有趣,所以我下载了vlcpython 模块并对其进行了修改,我想我找到了适合你的解决方案。

我所做的是我构建了一个包含 VLC 实例的数组,我构建了一个创建 VLC 实例的函数,以及一个确保它们正在运行(并产生详细输出)的循环。

这确实需要您确保您的 VLC 播放器允许多个窗口实例。

我的代码:

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")

然后你会在这里拨打所有电话或其他任何电话,然后循环:

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

结果输出看起来像这样,并且会有两个非窗口 VLC​​ 播放器。

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
于 2020-12-30T06:59:49.487 回答
0

主题的变体,使用多个实例vlc.Instance是要走的路。

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
        
于 2020-12-31T13:13:46.240 回答