Search code examples
pythonraspberry-pivlc

python-vlc switching playback media between launches


I'm trying to set up a system where my start-screen video loops until 1 of 2 buttons is pressed (GPIO buttons).

Then, the playback changes to either a video with subtitles or no-subtitles. Once that has finished its play-through, it reverts back to the splash screen video.

I have additional tickers in here just to count the number of play-throughs per day for analytics. My Test device also only has 1 button hooked up which is why GPIO 18 is never used. Implementation will be identical to GPIO 17's, so once one is working the other won't be hard to match up.

Problem

When I launch the script, the media played is not always splash. The script also closes the window at the end of playback, and opens a new one to play the media. I believe this may be due to not establishing an xwindow (for raspberry pi).

Any advice?

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

Solution

  • So I figured out the solution, but not the problem's origin.

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

    Setting up each of the media by name in my list helps by switching the play function from play_item_at_index(int) to play_item(media)

    Still not sure why it was kind of randomizing. My guess was that it changed the position of media in the list based on play through.

    My next step will be to adjust this to work off of media_player and embedding playback into a tkinter window.