I've got a vlc python program that I'd like load up a media list with multiple elements. If a clip (A) that I've defined as 'primary' is playing, it simply repeats. When I interrupt that with a different clip (B), I want to jump seamlessly to that clipB, then when clipB ends, jump back to the time that clipA left off. This will happen in fullscreen on a raspi4.
Here's the class for this so far (I've removed the fullscreen call for the moment):
from Colleague import Colleague
import vlc
class MyPlayer(Colleague):
def __init__(self, mediator):
super().__init__(mediator)
# init the list
self.media_list = vlc.MediaList()
# make the players
self.initializeVLCElements()
# create clip end data
self.ClipFinishedData = {'previousMediaIndex': 0, 'previousStopTime': 0}
def addMedia(self, mediaElement):
"""Add media to the media list as it gets passed in, then log the index
"""
# Set the index
mediaElement.setIndex(self.media_list.count())
# get the media
new_media = vlc.Media(mediaElement.getPath())
new_media.parse()
self.media_list.add_media(new_media)
def initializeVLCElements(self):
self.main_instance = vlc.Instance()
# create list player
self.list_player = self.main_instance.media_list_player_new()
# assign list to list player
self.list_player.set_media_list(self.media_list)
self.list_player.set_playback_mode(2)
# get the list player media player
self.media_player = self.list_player.get_media_player()
# set up event managers
events = self.media_player.event_manager()
events.event_attach(vlc.EventType.MediaPlayerEndReached, self.clipFinishedEvent)
def playMediaElement(self, mediaElement):
if mediaElement.getType() in ['default', 'primary']:
self.playPrimaryElement(mediaElement.getIndex())
else:
self.playOverlayElement(mediaElement.getIndex())
def playPrimaryElement(self, index):
logging.info('play primary')
self.list_player.play_item_at_index(index)
self.ClipFinishedData['previousMediaIndex'] = index
self.ClipFinishedData['previousStopTime'] = 0
def playOverlayElement(self, index):
logging.info('play overlay')
self.ClipFinishedData['previousStopTime'] = self.media_player.get_time()
self.list_player.play_item_at_index(index)
print('Prev video stopped at: ', self.ClipFinishedData['previousStopTime'])
def restartPreviousElement(self):
print(self.media_player.get_state()) # <-- this prints State.Stopped
#### program hangs here...
self.list_player.play_item_at_index(self.ClipFinishedData['previousMediaIndex'])
self.media_player.set_time(self.ClipFinishedData['previousStopTime'])
self.ClipFinishedData['previousStopTime'] = 0
self.ClipFinishedData['loopCurrent'] = True
def clipFinishedEvent(self, event):
self.restartPreviousElement()
I'm logging the departure time from the prior media, then on a clipFinishedEvent I'm restarting. If it's an overlay - I don't log the departure time and index in the playlist, so it should go back, otherwise the index is logged and the time is set to zero to repeat the media.
If I kill the player or create a new one, there's a flash frame, or other issue and I'd much prefer this to be just jumping around the playlist.
Any help here would be great.