foobar443
foobar443

Reputation: 2657

How can I refresh a slideshow when folder content changes?

I use a Raspberry Pi to show a slideshow of pictures stored in a folder. If I add pictures to the folder while the slideshow is running, the new pictures are not added to the slideshow.

How can I achieve this without interrupting the slideshow?

Upvotes: 0

Views: 1623

Answers (2)

foobar443
foobar443

Reputation: 2657

Add an autostart script to Kodi. It will run the slideshow automatically when Kodi starts and refresh the slideshow if the content of the folder changes:

# ~/.kodi/userdata/autoexec.py
# On Kodi startup, automatically start a Slideshow
# Refresh the slideshow if the content of the folder has been updated

import xbmc
from os import listdir
from time import sleep

xbmc.log('[refresh-slideshow] Starting')

monitor = xbmc.Monitor()
picfolder = "/PATH/TO/FOLDER"

# Generate list of images and start slideshow
l1 = listdir(picfolder)
xbmc.executebuiltin('SlideShow(%s)' %(picfolder))
xbmc.log('[refresh-slideshow] Slideshow started, monitoring')

# Wait for slideshow to start
sleep(5)

# Monitor during slideshow
while not monitor.abortRequested():
    # If slideshow is still running, compare directory contents
    if xbmc.getCondVisibility('Window.IsActive(slideshow)'):
        l2 = listdir(picfolder)
        # Restart slideshow if directory contents have changed
        if l1 != l2:
            xbmc.log('[refresh-slideshow] Folder contents changed, restarting slideshow')
            xbmc.executebuiltin('SlideShow(%s)' %(picfolder))
            l1 = l2
    # If slideshow is no longer running (user has exited), exit script
    else:
        break

    # Wait for 60 seconds, break if abort is requested
    if monitor.waitForAbort(60):
        break

xbmc.log('[refresh-slideshow] Finished, exiting')

Upvotes: 0

Rachit kapadia
Rachit kapadia

Reputation: 701

Convert your screensaver script to class based if it is not. Create another class to update images using threading:

import xbmc, xbmcgui, threading

class MyMonitor(xbmc.Monitor):
    def __init__(self, *args, **kwargs):
    self.action = kwargs['action']
    # do some action here.......

    def onScreensaverDeactivated(self):
        self.action()

class ImgVideoUpdate(threading.Thread):
    def __init__(self, *args, **kwargs):
        self._get_items = kwargs['data']
        threading.Thread.__init__(self)
        self.stop = False
        self.check_update = 50  # set thread to update images(in seconds)
        self.Monitor = MyMonitor(action=self._exit)

    def run(self):
        while (not self.Monitor.abortRequested()) and (not self.stop):
            count = 0
            while count != self.check_update:  # check for new images every 50seconds
                xbmc.sleep(200)
                count += 1
                if self.Monitor.abortRequested() or self.stop:
                    return
            self._get_items()

Now initialize ImgVideoUpdate from the ScreensaverWindow class from where the screensaver is running:

class ScreensaverWindow(xbmcgui.WindowXMLDialog):
    def onInit(self):
        xbmcgui.WindowXML.onInit(self)
        self.volumeCtrl = None
        # Get the videos to use as a screensaver
        playlist = self.imagelist()
        thread = ImgVideoUpdate(data=self.imagelist)
        thread.start()

Upvotes: 0

Related Questions