From 52bd5b662ed26f9f1ac417e1e00fdca9b3f80c08 Mon Sep 17 00:00:00 2001
From: shaun <shaun@bluebit.com.au>
Date: Mon, 30 Mar 2015 21:01:26 +1100
Subject: [PATCH] wait for the data to be available on PlaybackStarted event

---
 resources/lib/KodiMonitor.py |  2 +-
 resources/lib/Player.py      | 22 +++++++++++++++++-----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/resources/lib/KodiMonitor.py b/resources/lib/KodiMonitor.py
index 1b5af7a0..19a32d1d 100644
--- a/resources/lib/KodiMonitor.py
+++ b/resources/lib/KodiMonitor.py
@@ -29,7 +29,7 @@ class Kodi_Monitor(xbmc.Monitor):
         host = addon.getSetting('ipaddress')
         server = host + ":" + port
         downloadUtils = DownloadUtils()
-        #print "onNotification:" + method + ":" + sender + ":" + str(data)
+        print "onNotification:" + method + ":" + sender + ":" + str(data)
         #player started playing an item - 
         if method == "Player.OnPlay":
             print "playlist onadd is called"
diff --git a/resources/lib/Player.py b/resources/lib/Player.py
index db127b9f..5945345a 100644
--- a/resources/lib/Player.py
+++ b/resources/lib/Player.py
@@ -198,12 +198,22 @@ class Player( xbmc.Player ):
         
         if xbmcplayer.isPlaying():
             currentFile = xbmcplayer.getPlayingFile()
-            self.printDebug("emby Service -> onPlayBackStarted" + currentFile,2)
-                       
+            self.printDebug("emby Service -> onPlayBackStarted : " + currentFile, 0)
+            
+            # we may need to wait until the info is available
+            item_id = WINDOW.getProperty(currentFile + "item_id")
+            tryCount = 0
+            while(item_id == None or item_id == ""):
+                xbmc.sleep(500)
+                item_id = WINDOW.getProperty(currentFile + "item_id")
+                tryCount += 1
+                if(tryCount == 20): # try 20 times or about 10 seconds
+                    return
+            xbmc.sleep(500)
+            
             # grab all the info about this item from the stored windows props
             # only ever use the win props here, use the data map in all other places
             runtime = WINDOW.getProperty(currentFile + "runtimeticks")
-            item_id = WINDOW.getProperty(currentFile + "item_id")
             refresh_id = WINDOW.getProperty(currentFile + "refresh_id")
             audioindex = WINDOW.getProperty(currentFile + "AudioStreamIndex")
             subtitleindex = WINDOW.getProperty(currentFile + "SubtitleStreamIndex")
@@ -214,6 +224,7 @@ class Player( xbmc.Player ):
                 PlaybackUtils().seekToPosition(int(seekTime))
             
             if(item_id == None or len(item_id) == 0):
+                self.printDebug("emby Service -> onPlayBackStarted : No info for current playing file", 0)
                 return
         
             url = ("http://%s:%s/mediabrowser/Sessions/Playing" % (addonSettings.getSetting('ipaddress'), addonSettings.getSetting('port')))  
@@ -231,6 +242,7 @@ class Player( xbmc.Player ):
             if(subtitleindex != None and subtitleindex!=""):
               url = url + "&SubtitleStreamIndex=" + subtitleindex
             
+            self.printDebug("emby Service -> Sending Post Play Started : " + url, 0)
             self.downloadUtils.downloadUrl(url, postBody="", type="POST")   
             
             # save data map for updates and position calls
@@ -245,8 +257,8 @@ class Player( xbmc.Player ):
             data["Type"] = itemType
             self.played_information[currentFile] = data
             
-            self.printDebug("emby Service -> ADDING_FILE : " + currentFile)
-            self.printDebug("emby Service -> ADDING_FILE : " + str(self.played_information))
+            self.printDebug("emby Service -> ADDING_FILE : " + currentFile, 0)
+            self.printDebug("emby Service -> ADDING_FILE : " + str(self.played_information), 0)
 
             # log some playback stats
             if(itemType != None):