python-vlc 在启动之间切换播放媒体

python-vlc switching playback media between launches

我正在尝试设置一个系统,让我的开始屏幕视频循环播放,直到按下 2 个按钮中的 1 个(GPIO 按钮)。

然后,播放变为有字幕或无字幕的视频。 完成播放后,它会恢复到启动画面视频。

我在这里有额外的代码,只是为了计算每天的播放次数以供分析。我的测试设备也只连接了 1 个按钮,这就是 GPIO 18 从未被使用的原因。实施将与 GPIO 17 相同,因此一旦一个工作正常,另一个将不难匹配。

问题

当我启动脚本时,播放的媒体并不总是 splash。该脚本还在播放结束时关闭 window,并打开一个新的播放媒体。我相信这可能是由于没有建立 xwindow(对于 raspberry pi)。

有什么建议吗?

#Vars

GPIO.setmode(GPIO.BCM)

GPIO.setup(17,GPIO.IN)
GPIO.setup(18,GPIO.IN)


update = True #Update to false to exit

def Main():
    # Setup logs
    print(date.today())
    # Media Paths
    path = "/home/pi/Videos/"
    nosubs = path+"Content-NoSubs.mp4"
    subs = path+"Content-Subtitles.mp4"
    splash = path+"StartScreen.mp4"
    Instance = vlc.Instance("-f")
    playlist = set([splash,subs,nosubs])
    url = [str(splash),str(subs),str(nosubs)] #Yes, this looks pretty redundant. Hopefully it's not.



    #Setup the player
    player = Instance.media_list_player_new()
    Media = Instance.media_new(url[1])
    Media_list = Instance.media_list_new(playlist)
    Media.get_mrl()
    player.set_media_list(Media_list)

    playerState = {'State.NothingSpecial',
 'State.Opening',
 'State.Buffering',
 'State.Playing',
 'State.Paused',
 'State.Stopped',
 'State.Ended',
 'State.Error'}

    subsPlayed = 0
    nosubsPlayed = 0
    active = 0
    playingMedia = 0

    while update:
        input = GPIO.input(17)
        state = str(player.get_state())

        if(state == playerState[0]):
            player.play_item_at_index(0)
            player.set_playback_mode(2)
    
        if(state == playerState[7]):
            player.play_item_at_index(0)
            playingMedia = 0
        
        if input == 1 and playingMedia == 0:
            playingMedia = 1
            player.play_item_at_index(1)
            active +=1
            nosubsPlayed +=1
        
        print(playingMedia)

    with open(str(date.today()))+'.txt','w' as file:
        file.write("Active Views: " + active)
        file.write("SubsPlayed: " + subsPlayed)
        file.write("No Subs Played: " + nosubsPlayed)
    
Main()

所以我找到了解决方案,但没有找到问题的根源。


    # Make my media paths into vlc.Media Objects
    nosubs = vlc.Media(path+"Content-NoSubs.mp4")
    subs = vlc.Media(path+"Content-Subtitles.mp4")
    splash = vlc.Media(path+"SplashScreen.mp4")

    #Setup the player
    player = Instance.media_list_player_new()
    Media_list = Instance.media_list_new()
    Media_list.add_media(splash)
    Media_list.add_media(subs)
    Media_list.add_media(nosubs)
    player.set_media_list(Media_list)
    Media_list.lock()

在我的列表中按名称设置每个媒体有助于将播放功能从 play_item_at_index(int) 切换到 play_item(media)

仍然不确定为什么这是一种随机化。我的猜测是它根据播放时间改变了媒体在列表中的位置。

我的下一步将是调整它以关闭 media_player 并将播放嵌入到 tkinter window。