我正在开发一个 tkinter 应用程序,其中视频和声音根据某些事件进行排队和播放。
为此,我使用 python-vlc,但我还没有找到同时播放多个声音的方法(除了多线程)。视频还需要自己的窗口才能正常工作。
以下代码来自纯粹用于测试此功能的模块。
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 中的某些功能?
您可以,但需要单独的实例。
这是使用列表的一种简单且相当粗糙的方法。我相信随着时间的推移,您可以设计出一种更干净的方法。
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
附注感谢版主删除了我对这个问题的另一个答案的质量的评论。您是否容忍“阅读手册”类型的答案或评论?你太无耻了。有了这种态度,这个网站就会失败。 我等待您的
surreptitious
编辑。
使用 mpyg321 执行此操作:
pip3 install mpyg321
from mpyg321.mpyg321 import MPyg321Player
然后:
while True:
player = MPyg321Player()
player.play_song(input('Path To Song'))
谢谢
你成功了吗?我一直在尝试在 Raspberry Pi 上做同样的事情。我在这里询问但没有解决。昨天我成功实现了目标。我可以在 python 中播放视频,当 PIR 运动检测器检测到运动时,它会切换到第二个视频。 我正在使用 python-vlc。 这是我的代码:
# motion_player_new
# A short program that plays a video and switches to a second video when motion is detected.
import vlc
from gpiozero import MotionSensor
from time import sleep
# Initialise VLC player
instance = vlc.Instance()
player = instance.media_player_new()
# Create media objects
media1 = instance.media_new('video1.mp4')
media2 = instance.media_new('video2.mp4')
# Set media to player
player.set_media(media1)
# Initialise pir
pir = MotionSensor(23)
#player.play()
# Flag to track if motion is detected
motion_detected = False
try:
while True:
# Check for motion
if pir.motion_detected:
if not motion_detected:
motion_detected = True
print("Motion detected")
# Play second video
player.set_media(media2)
player.play()
while player.get_state() != vlc.State.Ended:
sleep(1)
# Reset to first video
player.set_media(media1)
player.play()
else:
motion_detected = False
sleep(0.1) # wait before checking again
except KeyboardInterrupt:
print("Exiting player....")
player.stop()
如果你想使用 python-vlc
您可以使用“Thread”并在后台播放音乐或......。
import threading
def MusicPlay(Name):
#Your Code
while True:
threading.Thread(target=Delete, args=('NameOfMusic',)).start()