From 6164f48cb95e93adc77cc8cecdb929ac3da33fc5 Mon Sep 17 00:00:00 2001 From: poly000 <34085039+poly000@users.noreply.github.com> Date: Wed, 24 Jan 2024 11:20:36 +0800 Subject: [PATCH] fix: windows `SystemMediaTransportControls` integration (#755) --- feeluown/nowplaying/nowplaying.py | 35 +++++++++++++++++++++---------- feeluown/plugin.py | 7 ++++++- setup.py | 4 ++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/feeluown/nowplaying/nowplaying.py b/feeluown/nowplaying/nowplaying.py index a7ac36a11a..518dcdf925 100644 --- a/feeluown/nowplaying/nowplaying.py +++ b/feeluown/nowplaying/nowplaying.py @@ -1,5 +1,6 @@ # pylint: disable=import-error import logging +import os import aionowplaying as aionp @@ -19,12 +20,12 @@ } -def to_aionp_time(t): - return int(t * 1000) +def sec_to_us(t_sec): + return int(t_sec * 1000 * 1000) -def from_aionp_time(t): - return t / 1000 +def us_to_sec(t_microsec): + return t_microsec / 1000 / 1000 class NowPlayingService(aionp.NowPlayingInterface): @@ -32,6 +33,9 @@ def __init__(self, app: 'ServerApp'): super().__init__('FeelUOwn') self._app = app + if os.name == "nt": + self._app.player.position_changed.connect(self.update_position) + self._app.player.seeked.connect(self.update_position) self._app.player.media_loaded.connect(self.on_player_media_loaded, aioqueue=True) @@ -53,6 +57,7 @@ def __init__(self, app: 'ServerApp'): self.set_playback_property(PlayProp.CanGoPrevious, True) self.set_playback_property(PlayProp.CanSeek, True) self.set_playback_property(PlayProp.CanControl, True) + self.set_playback_property(PlayProp.Rate, 1.0) def update_playback_mode(self, mode: PlaybackMode): mode_mapping = { @@ -71,14 +76,16 @@ def update_song_metadata(self, meta: dict): metadata.album = meta.get('album', '') metadata.title = meta.get('title', '') metadata.cover = meta.get('artwork', '') - metadata.url = '' + metadata.url = meta.get('uri', '') + if os.name == 'nt': + metadata.id_ = meta.get('uri', '') self.set_playback_property(PlayProp.Metadata, metadata) def update_duration(self, duration): - self.set_playback_property(PlayProp.Duration, int(duration * 1000)) + self.set_playback_property(PlayProp.Duration, int(sec_to_us(duration or 0))) def update_position(self, position): - self.set_playback_property(PlayProp.Position, int(position * 1000)) + self.set_playback_property(PlayProp.Position, int(sec_to_us(position or 0))) def update_playback_status(self, state): self.set_playback_property(PlayProp.PlaybackStatus, @@ -96,6 +103,9 @@ async def on_next(self): async def on_previous(self): self._app.playlist.previous() + def on_stop(self): + self._app.player.stop() + def on_loop_status(self, status: aionp.LoopStatus): if status == aionp.LoopStatus.Playlist: self._app.playlist.playback_mode = PlaybackMode.loop @@ -110,14 +120,17 @@ def on_shuffle(self, shuffle: bool): else: self._app.playlist.playback_mode = PlaybackMode.sequential - def on_seek(self, offset: int): - self._app.player.position = from_aionp_time(offset) + def on_set_position(self, track_id: str, position: int): + self._app.player.position = us_to_sec(position) + + def on_seek(self, offset_us: int): + self._app.player.position = us_to_sec(offset_us) def get_playback_property(self, name: PlayProp): if name == PlayProp.Duration: - return to_aionp_time(self._app.player.duration) + return sec_to_us(self._app.player.duration) elif name == PlayProp.Position: - return to_aionp_time(self._app.player.position) + return sec_to_us(self._app.player.position) elif name == PlayProp.PlaybackStatus: return StatePlaybackStatusMapping[self._app.player.state] elif name == PlayProp.Rate: diff --git a/feeluown/plugin.py b/feeluown/plugin.py index 0666add118..e7e358775e 100644 --- a/feeluown/plugin.py +++ b/feeluown/plugin.py @@ -202,7 +202,12 @@ def _scan_entry_points(self): """ try: import importlib.metadata # pylint: disable=redefined-outer-name - entry_points = importlib.metadata.entry_points().get('fuo.plugins_v1', []) + try: + entry_points = importlib.metadata.entry_points() \ + .select(group='fuo.plugins_v1') + except AttributeError: # old version does not has `select` method + entry_points = importlib.metadata.entry_points() \ + .get('fuo.plugins_v1', []) except ImportError: import pkg_resources entry_points = pkg_resources.iter_entry_points('fuo.plugins_v1') diff --git a/setup.py b/setup.py index e25d6ced31..bf1b491fa8 100644 --- a/setup.py +++ b/setup.py @@ -69,11 +69,11 @@ 'feeluown-bilibili>=0.3.1', ], 'macOS': [ - 'aionowplaying>=0.9.4,<0.10', + 'aionowplaying>=0.10', ], 'win32': [ 'pyshortcuts', - 'aionowplaying>=0.9.4,<0.10', + 'aionowplaying>=0.10', ], 'webserver': ['sanic', 'websockets', 'json-rpc'], 'webengine': ['PyQtWebEngine'],