0

所以我正在使用 VLC python 扩展。我正在尝试将播放视频与另一个程序对齐。由于 VLC 媒体播放器在加载媒体和开始播放所需的时间方面存在一些差异,因此有时可能会不对齐。我想在 VLC 中打开/加载视频,让它暂停,然后等待触发器播放,这样我就可以用我的程序的另一部分执行命令。

当我将值放入 media_player.pause 时,视频不会播放,但其他进程会。如果我没有在 media_player.pause() 中输入任何值,视频将不间断地播放

import time, vlc
    
# media object
media = vlc.Media('video.mp4')
  
# setting media to the media player
media_player.set_media(media)
media_player = vlc.MediaPlayer()
media_player.set_media(media)

# Play to open/load the video
media_player.play()

# Pause the Video
media_player.pause()

# Other Stuff Happens

time.sleep(init_delay)
media_player.play()
4

1 回答 1

0

You need to wait for the video to load or pre-roll before it will play.
It will not pause before it is playing.
The simple answer is to allow the video to get to a "playing" state and then pause it.
This can be done, simply by allowing it time to get to the playing state, by sleeping, for say 1 tenth of a second.
You don't indicate what the activation trigger is, so in the following example, I've added a Linux friendly, keyboard trigger, that times-out after 10 seconds.

import time, vlc
import sys, select
    
# media object
media_player = vlc.MediaPlayer()
media = vlc.Media('V1.mp4')
vlc_playing = set([3, 4]) #  3 - Playing | 4 - Paused
media_state = None
# setting media to the media player
media_player.set_media(media)

# Play to open/load the video
media_player.play()
while media_state not in vlc_playing:
    time.sleep(0.1)
    media_state = media_player.get_state()

media_player.pause()
print("Paused - waiting for trigger")
print("Activate trigger with Enter or Ctrl+C to terminate")

#  Here the activation trigger is provide by pressing the Enter key
#  within 10 seconds
#  I've no idea if this will work on a Non Linux box
#
try:
    inp, out, err = select.select( [sys.stdin], [], [], 10 )
    if inp:
        media_player.play()
    else:
        media_state = None
except KeyboardInterrupt:
    media_state = None

if media_state: # Play loop
    while media_state in vlc_playing:
        time.sleep(1)
        try: # Cater for Ctrl+C during Play
            media_state = media_player.get_state()
        except Exception:
            media_state = None
        continue
else:
    print("No Activation trigger - Terminated")
于 2021-10-22T15:46:12.750 回答