Try to start this script, using your own .mp4 video, then open another app (fi Safari) without stopping Pythonista, and come back in a few minutes, you should see in the ui.View title the number of seconds the script has run.
import ui
import sys
import time
import os
class MyPictureInPicture(ui.View):
def __init__(self,main_view, movie_path):
self.main_view = main_view
self.frame = (0,0,10,10)
self.hidden = True
self.web = ui.WebView()
self.add_subview(self.web)
self.main_view.add_subview(self)
mov_url = 'file://' + os.path.expanduser(movie_path)
html = '''
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<video video {min-height: 400px;} id="videoplayer" loop controls="" autoplay="">
<source src="mov_url" type="video/mp4">
</video>
</body>
'''
html = html.replace('mov_url',mov_url)
self.web.load_html(html)
def set_PiP(self):
time.sleep(0.2)
# wait for it to complete
while self.web.eval_js('document.readyState')!='complete':
time.sleep(0.1)
self.web.eval_js('videoplayer=document.getElementById("videoplayer");')
self.web.eval_js('videoplayer.play();')
time.sleep(0.5)
self.web.eval_js('videoplayer.webkitSetPresentationMode("picture-in-picture")')
self.main_view.remove_subview(self)
class MyView(ui.View):
def __init__(self):
self.frame = (0,0,400,400)
self.background_color = 'yellow'
self.n = 0
self.update_interval = 1
def update(self):
self.n += 1
self.name = 'Test runs for '+str(self.n)+' secs'
# Protect against import
if __name__ == '__main__':
mv = MyView()
mv.present('fullscreen')
myPiP = MyPictureInPicture(mv, '~/Documents/Pythonista_runs_in_background.mp4')
myPiP.set_PiP()

