python question: how to check when media is playing
#1
Hi, I looked through the wiki to no avail. I have a main box serving its library to several RPis. I'm using the WOL function to turn it on, but I'm wanting to set the idle shutdown timer on that main box so I don't waste power when nothing is streaming. I'm writing an addon to run on the RPis that sends an inhibit sleep command to the box serving the media whenever they are accessing media. I'm trying to find a way I can check if the pi is currently streaming media from that particular source. I some times turn them on just to stream from addons so in that instance I don't want to inhibit sleep. Any help or suggestions is greatly appreciated.
Reply
#2
trying to do this client side is silly. the server knows if it serves files.
Reply
#3
xbmc.Player().isPlaying() will return true if the instance of Kodi on which you run the code is playing something. xbmc.Player().isPlayingAudio() will return true if it's playing audio, and xvmc.Player().isPlayingVideo() will return true if it's playing a video file.
Reply
#4
Same for Boolean conditions, which give a bit more flexibility:

Code:
xmbc.getCondVisibility('Player.HasMedia') -> returns true if kodi is playing anything
xbmc.getCondVisibility('Player.HasVideo') -> returns true if kodi is playing a videofile
xbmc.getCondVisibility('Player.HasAudio') -> returns true if kodi is playing an audio file
xbmc.getCondVisibility('Player.IsInternetStream') -> returns true if kodi is playing an internet stream
xbmc.getCondVisibility('Pvr.IsPlayingTv') -> returns true if kodi is playing tv (from pvr)
xbmc.getCondVisibility('Pvr.IsPlayingRadio') -> returns true if kodi is playing radio (from pvr)
xbmc.getCondVisibility('Pvr.IsPlayingRecording') -> returns true if kodi is playing a recording (from pvr)

Source: http://kodi.wiki/view/List_of_boolean_conditions
Reply
#5
(2015-10-21, 14:16)enen92 Wrote: Same for Boolean conditions, which give a bit more flexibility:

Code:
xmbc.getCondVisibility('Player.HasMedia') -> returns true if kodi is playing anything
xbmc.getCondVisibility('Player.HasVideo') -> returns true if kodi is playing a videofile
xbmc.getCondVisibility('Player.HasAudio') -> returns true if kodi is playing an audio file
xbmc.getCondVisibility('Player.IsInternetStream') -> returns true if kodi is playing an internet stream
xbmc.getCondVisibility('Pvr.IsPlayingTv') -> returns true if kodi is playing tv (from pvr)
xbmc.getCondVisibility('Pvr.IsPlayingRadio') -> returns true if kodi is playing radio (from pvr)
xbmc.getCondVisibility('Pvr.IsPlayingRecording') -> returns true if kodi is playing a recording (from pvr)

Source: http://kodi.wiki/view/List_of_boolean_conditions

My code isn't exactly functional. In theory it should be properly formatted. The first version utilizes the boolean and the second version utilizes another command to get the same results. Both do not work.

I have a button hooked up that runs this code but the if command seems to break the entirety of it. The script will still run but just won't do anything.


Version 1
Code:
if(xmbc.getCondVisibility("Player.HasMedia") == True):
            xbmc.executebuiltin("Action(SkipNext)")
        else:
            xbmc.executebuiltin("Action(Down)")

Version 2
Code:
path3 = xbmc.Player().isPlayingVideo()
        if(path3 == True):
            xbmc.executebuiltin("Action(SkipNext)")
        else:
            #xbmc.executebuiltin("Action(Down)")
Reply
#6
try this and check the xbmc.log file. if nothing gets logged, than the problem is likely elsewhere in your code.

Code:
if xmbc.getCondVisibility("Player.HasMedia"):
    print '===TRUE==='
    xbmc.executebuiltin("Action(SkipNext)")
else:
    print '===FALSE==='
    xbmc.executebuiltin("Action(Down)")
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#7
I think the issue is as soon as a video plays the code stops. You need to keep the scrip alive and test if video is playing at certain intervals.
Reply
#8
(2016-04-13, 11:56)ronie Wrote: try this and check the xbmc.log file. if nothing gets logged, than the problem is likely elsewhere in your code.

Code:
if xmbc.getCondVisibility("Player.HasMedia"):
    print '===TRUE==='
    xbmc.executebuiltin("Action(SkipNext)")
else:
    print '===FALSE==='
    xbmc.executebuiltin("Action(Down)")

Sorry for getting back so late but yes it doesn't seem to get logged. Could you take a look at my code to see if there would be anything obvious not letting it work?

A link to the log, http://xbmclogs.com/paj3nburd

I have also tried to have the button directly print without an if and it doesn't seem to do that either.

Code:
#!/usr/bin/python

import sys
sys.path.append('/storage/.kodi/addons/python.RPi.GPIO/lib')

import RPi.GPIO as GPIO
import xbmc
import xbmcgui

--------------------------------- (MISSING CODE THAT SET UPS WHICH BUTTON IS CONNECTED TO WHICH GPIO)

def up_callback(channel): #NOT USED
        xbmc.executebuiltin("Action(Up)")
    
def down_callback(channel): #NOT USED
        xbmc.executebuiltin("Action(Down)")
      
def left_callback(channel):
        xbmc.executebuiltin("Action(SkipPrevious)")

def right_callback(channel):
        if xmbc.getCondVisibility("Player.HasMedia"):
            print '===TRUE==='
            xbmc.executebuiltin("Action(SkipNext)")
        else:
            print '===FALSE==='
            xbmc.executebuiltin("Action(Down)")
        

#def select_callback(channel):
        #xbmc.executebuiltin("Action(Right)")
        
def PlayNPause_callback(channel):
        #xbmc.executebuiltin("Action(Select)")
        xbmc.executebuiltin("Action(PlayPause)")
        
def BackPin_callback(channel): #NOT USED
        xbmc.executebuiltin("Action(Stop)")
        
def Home_callback(channel): #NOT USED
        xbmc.executebuiltin("ActivateWindow(10000)")

Some of the code is missing the forum apparently thinks it is malicious so I don't know what to do about that.
Reply
#9
(2015-10-20, 01:41)pkscout Wrote: xbmc.Player().isPlaying() will return true if the instance of Kodi on which you run the code is playing something. xbmc.Player().isPlayingAudio() will return true if it's playing audio, and xvmc.Player().isPlayingVideo() will return true if it's playing a video file.

Hi,
I am using Kodi in my windows 10. While I was trying xvmc.Player().isPlayingVideo() as you mentioned I have faced a problem. When I run a video xvmc.Player().isPlayingVideo() is returning true, but when I close the video it still shows true(1). Am I doing anything wrong?
Thank you
Reply

Logout Mark Read Team Forum Stats Members Help
python question: how to check when media is playing0