From 10e3f21f262cb3fc3b8de6bc1393ac0fc02fdd8b Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Tue, 7 Dec 2021 22:19:20 +0100 Subject: [PATCH] Working cast with, pause and play --- ChannelProvider/SVT/__init__.py | 2 - main.py | 81 +++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/ChannelProvider/SVT/__init__.py b/ChannelProvider/SVT/__init__.py index 27352e9..25f4eed 100644 --- a/ChannelProvider/SVT/__init__.py +++ b/ChannelProvider/SVT/__init__.py @@ -22,7 +22,6 @@ class SVT(ChannelProvider.ChannelProvider): super().__init__('SVT', 'http://www.svtplay.se/rss.xml') if os.path.exists(self.m_cachefile): - print("Found cache file: {}".format(self.m_cachefile)) with open(self.m_cachefile, 'rb') as cachehandle: self.m_cache = pickle.load(cachehandle) self.m_items: list[Item] = self.parse_feed() @@ -39,7 +38,6 @@ class SVT(ChannelProvider.ChannelProvider): key = hashlib.sha256(entry['link'].encode('utf-8')).hexdigest() if key in self.m_cache.keys(): - print("Cache hit: {}".format(key)) thumbnail_link = self.m_cache[key]['thumbnail_link'] resolved_link = self.m_cache[key]['resolved_link'] description = self.m_cache[key]['description'] diff --git a/main.py b/main.py index 380931d..3516750 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 +import time from typing import NewType +import pychromecast import wx import wx.lib.scrolledpanel as scrolled import wx.media @@ -11,12 +13,15 @@ ChannelProvider = NewType('ChannelProvider', ChannelProvider) class Cast(wx.Frame): + m_selected_chromecast = None + def __init__(self, *args, **kw): """__init__. :param args: :param kw: """ super().__init__(*args, **kw) + self.m_chromecasts, self.m_browser = pychromecast.get_chromecasts() self.m_index = 0 self.m_sizer: wx.Sizer = wx.BoxSizer(wx.VERTICAL) self.m_panel: wx.lib.scrolledpanel.ScrolledPanel = scrolled.ScrolledPanel( @@ -31,14 +36,15 @@ class Cast(wx.Frame): self.m_sizer.Clear(delete_windows=True) self.m_sizer = wx.BoxSizer(wx.VERTICAL) - for channel in self.m_channels: for item in channel.get_items(): title = wx.StaticText(self.m_panel, -1, item['title']) description = wx.StaticText(self.m_panel, -1, item['description']) bitmap = item['thumbnail'] - btn = wx.BitmapButton(self.m_panel, id=self.m_index, bitmap=bitmap) + btn = wx.BitmapButton(self.m_panel, + id=self.m_index, + bitmap=bitmap) btn.Bind(wx.EVT_BUTTON, lambda event, link=item['link']: self.show_player( event, link)) @@ -58,10 +64,8 @@ class Cast(wx.Frame): style=wx.SIMPLE_BORDER, szBackend=wx.media.MEDIABACKEND_GSTREAMER) play_button = wx.Button(self.m_panel, -1, "Play") - play_button.Bind(wx.EVT_BUTTON, self.play) pause_button = wx.Button(self.m_panel, -1, "Pause") - pause_button.Bind(wx.EVT_BUTTON, self.pause) back_button = wx.Button(self.m_panel, -1, "Back") back_button.Bind(wx.EVT_BUTTON, self.show_list) @@ -72,13 +76,80 @@ class Cast(wx.Frame): self.m_sizer.Add(pause_button, (1, 2)) self.m_sizer.Add(back_button, (1, 3)) - self.Bind(wx.media.EVT_MEDIA_LOADED, self.play) + if self.m_chromecasts and not self.m_selected_chromecast: + chromecast_button = wx.Button(self.m_panel, -1, "Cast") + chromecast_button.Bind( + wx.EVT_BUTTON, + lambda event, muri=uri: self.select_chromecast(event, muri)) + self.m_sizer.Add(chromecast_button, (2, 2)) + + if self.m_selected_chromecast: + self.Bind(wx.media.EVT_MEDIA_LOADED, + lambda event, muri=uri: self.cast(event, muri)) + play_button.Bind(wx.EVT_BUTTON, self.play_cast) + pause_button.Bind(wx.EVT_BUTTON, self.pause_cast) + else: + self.Bind(wx.media.EVT_MEDIA_LOADED, self.play) + play_button.Bind(wx.EVT_BUTTON, self.play) + pause_button.Bind(wx.EVT_BUTTON, self.pause) + self.Bind(wx.media.EVT_MEDIA_FINISHED, self.show_list) self.load_uri(uri) self.m_panel.SetSizer(self.m_sizer) self.m_panel.Layout() self.m_panel.ScrollChildIntoView(self.m_control) + def select_chromecast(self, _, uri): + self.m_sizer.Clear(delete_windows=True) + self.m_sizer = wx.BoxSizer(wx.VERTICAL) + + for cast in self.m_chromecasts: + friendly_name = cast.cast_info.friendly_name + btn = wx.Button(self.m_panel, id=-1, label=friendly_name) + btn.Bind(wx.EVT_BUTTON, + lambda event, chromecast=cast, muri=uri: self. + set_chromecast(event, chromecast, muri)) + self.m_sizer.Add(btn) + self.m_panel.SetSizer(self.m_sizer) + self.m_panel.Layout() + + def set_chromecast(self, event, chromecast, uri): + self.m_selected_chromecast = chromecast + self.show_player(event, uri) + + def cast(self, event, uri): + mimetype = 'video/mp4' + cast = self.m_selected_chromecast + cast.wait() + cast.media_controller.play_media(uri, mimetype) + # Wait for player_state PLAYING + player_state = None + has_played = False + + while True: + if player_state != cast.media_controller.status.player_state: + player_state = cast.media_controller.status.player_state + print("Player state:", player_state) + + if player_state == "PLAYING": + has_played = True + break + + if cast.socket_client.is_connected and has_played and player_state != "PLAYING": + has_played = False + cast.media_controller.play_media(uri, mimetype) + + time.sleep(0.1) + self.m_browser.stop_discovery() + + def pause_cast(self, event): + cast = self.m_selected_chromecast + cast.media_controller.pause() + + def play_cast(self, event): + cast = self.m_selected_chromecast + cast.media_controller.play() + def load_uri(self, uri): self.m_control.LoadURI(uri)