From c4608b38e131ecdf61d32b24ee0baa687bb00a39 Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Wed, 3 Feb 2016 19:06:12 -0600
Subject: [PATCH] Fix resume when using play-to

---
 resources/lib/kodimonitor.py |  3 +--
 resources/lib/player.py      | 11 +++++++++++
 resources/lib/playlist.py    | 18 +++++++-----------
 3 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py
index 05ecf8a5..ac781e5a 100644
--- a/resources/lib/kodimonitor.py
+++ b/resources/lib/kodimonitor.py
@@ -200,5 +200,4 @@ class KodiMonitor(xbmc.Monitor):
             utils.window('emby_onWake', value="true")
 
         elif method == "Playlist.OnClear":
-            utils.window('emby_customPlaylist', clear=True)
-            self.logMsg("Clear playlist properties.")
\ No newline at end of file
+            pass
\ No newline at end of file
diff --git a/resources/lib/player.py b/resources/lib/player.py
index 7972edec..354b8984 100644
--- a/resources/lib/player.py
+++ b/resources/lib/player.py
@@ -98,6 +98,15 @@ class Player(xbmc.Player):
                 itemType = utils.window("%s.type" % embyitem)
                 utils.window('emby_skipWatched%s' % itemId, value="true")
 
+
+                if (utils.window('emby_customPlaylist') == "true" and
+                        utils.window('emby_customPlaylist.seektime')):
+                    # Start at, when using custom playlist (play to Kodi from webclient)
+                    seektime = utils.window('emby_customPlaylist.seektime')
+                    self.logMsg("Seeking to: %s" % seektime, 1)
+                    xbmcplayer.seekTime(int(seektime)/10000000.0)
+                    utils.window('emby_customPlaylist.seektime', clear=True)
+
                 seekTime = xbmcplayer.getTime()
 
                 # Get playback volume
@@ -401,6 +410,7 @@ class Player(xbmc.Player):
         # Will be called when user stops xbmc playing a file
         self.logMsg("ONPLAYBACK_STOPPED", 2)
         utils.window('emby_customPlaylist', clear=True)
+        utils.window('emby_customPlaylist.seektime', clear=True)
         utils.window('emby_playbackProps', clear=True)
         self.logMsg("Clear playlist properties.", 1)
         self.stopAll()
@@ -408,6 +418,7 @@ class Player(xbmc.Player):
     def onPlayBackEnded( self ):
         # Will be called when xbmc stops playing a file
         self.logMsg("ONPLAYBACK_ENDED", 2)
+        utils.window('emby_customPlaylist.seektime', clear=True)
         self.stopAll()
 
     def stopAll(self):
diff --git a/resources/lib/playlist.py b/resources/lib/playlist.py
index 6b4c1e54..c265cb79 100644
--- a/resources/lib/playlist.py
+++ b/resources/lib/playlist.py
@@ -43,17 +43,19 @@ class Playlist():
         embycursor = embyconn.cursor()
         emby_db = embydb.Embydb_Functions(embycursor)
 
-        self.logMsg("---*** PLAY ALL ***---", 1)
-        self.logMsg("Items: %s and start at: %s" % (itemids, startat))
-
         player = xbmc.Player()
         playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
         playlist.clear()
-        started = False
 
+        self.logMsg("---*** PLAY ALL ***---", 1)
+        self.logMsg("Items: %s and start at: %s" % (itemids, startat))
+
+        started = False
         utils.window('emby_customplaylist', value="true")
 
-        position = 0
+        if startat != 0:
+            # Seek to the starting position
+            utils.window('emby_customplaylist.seektime', str(startat))
 
         for itemid in itemids:
             embydb_item = emby_db.getItem_byId(itemid)
@@ -75,12 +77,6 @@ class Playlist():
                 started = True
                 player.play(playlist)
 
-        if startat:
-            # Seek to the starting position
-            seektime = startat / 10000000.0
-            player.seekTime(seektime)
-            self.logMsg("Seeking to: %s" % seektime, 1)
-
         self.verifyPlaylist()
         embycursor.close()