Skip to content

Commit

Permalink
Merge pull request #11 from waylyrics/fix-smtc-support
Browse files Browse the repository at this point in the history
fix: windows SMTC support
  • Loading branch information
BruceZhang1993 authored Jan 12, 2024
2 parents 19f650c + 2c51370 commit d3a3289
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions aionowplaying/interface/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
MediaType


def TimeSpan(x):
return timedelta(microseconds=x/10000)
def TimeSpan(x_microsec):
return timedelta(microseconds=x_microsec)


class WindowsInterface(BaseInterface):
Expand Down Expand Up @@ -65,13 +65,19 @@ def playback_rate_change_requested(self, _, args: PlaybackRateChangeRequestedEve

def playback_position_change_requested(self, _, args: PlaybackPositionChangeRequestedEventArgs):
position = args.requested_playback_position
position = position.seconds * 1000 * 1000 + position.microseconds

if self._playback_properties.CanSeek:
if asyncio.iscoroutinefunction(self.on_set_position):
self._run_task(self.on_set_position(self._playback_properties.Metadata.id_, int(position.duration / 10000)))
self._run_task(self.on_seek(int(position.duration / 10000)))
self._run_task(self.on_set_position(self._playback_properties.Metadata.id_, position))
else:
self.on_set_position(self._playback_properties.Metadata.id_, position)

if asyncio.iscoroutinefunction(self.on_seek):
self._run_task(self.on_seek(position))
else:
self.on_set_position(self._playback_properties.Metadata.id_, int(position.duration / 10000))
self.on_seek(int(position.duration / 10000))
self.on_seek(position)


def button_pressed(self, _, args: SystemMediaTransportControlsButtonPressedEventArgs):
button: SystemMediaTransportControlsButton = args.button
Expand Down Expand Up @@ -99,6 +105,11 @@ def button_pressed(self, _, args: SystemMediaTransportControlsButtonPressedEvent
self._run_task(self.on_previous())
else:
self.on_previous()
if button == SystemMediaTransportControlsButton.STOP and self._playback_properties.CanControl:
if asyncio.iscoroutinefunction(self.on_stop):
self._run_task(self.on_stop())
else:
self.on_stop()

def auto_repeat_mode_change_requested(self, _, args: AutoRepeatModeChangeRequestedEventArgs):
value = LoopStatus.None_
Expand All @@ -117,7 +128,10 @@ def set_property(self, name: PropertyName, value: Any):
pass

def set_playback_property(self, name: PlaybackPropertyName, value: Any):
if name == PlaybackPropertyName.CanPlay:
if name == PlaybackPropertyName.CanControl:
self._controls.is_stop_enabled = value
self._playback_properties.CanControl = value
elif name == PlaybackPropertyName.CanPlay:
self._controls.is_play_enabled = value
self._playback_properties.CanPlay = value
elif name == PlaybackPropertyName.CanPause:
Expand All @@ -129,6 +143,8 @@ def set_playback_property(self, name: PlaybackPropertyName, value: Any):
elif name == PlaybackPropertyName.CanGoPrevious:
self._controls.is_previous_enabled = value
self._playback_properties.CanGoPrevious = value
elif name == PlaybackPropertyName.CanSeek:
self._playback_properties.CanSeek = value
elif name == PlaybackPropertyName.PlaybackStatus:
if value == PlaybackStatus.Playing:
self._controls.playback_status = MediaPlaybackStatus.PLAYING
Expand All @@ -155,7 +171,11 @@ def set_playback_property(self, name: PlaybackPropertyName, value: Any):
self._controls.auto_repeat_mode = MediaPlaybackAutoRepeatMode.TRACK
self._playback_properties.LoopStatus = value
elif name == PlaybackPropertyName.Position:
self._timeline.position = TimeSpan(value * 10000)
self._timeline.position = TimeSpan(value)
self._controls.update_timeline_properties(self._timeline)
elif name == PlaybackPropertyName.Duration:
self._timeline.end_time = TimeSpan(value)
self._timeline.max_seek_time = TimeSpan(value)
self._controls.update_timeline_properties(self._timeline)

def _update_metadata(self, value: PlaybackProperties.MetadataBean):
Expand All @@ -166,7 +186,9 @@ def _update_metadata(self, value: PlaybackProperties.MetadataBean):
self._updater.type = MediaPlaybackType.VIDEO
else:
self._updater.type = MediaPlaybackType.MUSIC

self._updater.app_media_id = value.id_

self._updater.music_properties.artist = ','.join(value.artist)
self._updater.music_properties.title = value.title
self._updater.music_properties.album_title = value.album
Expand All @@ -178,9 +200,9 @@ def _update_metadata(self, value: PlaybackProperties.MetadataBean):
self._updater.update()
# update timeline
self._timeline.start_time = TimeSpan(0)
self._timeline.end_time = TimeSpan(value.duration * 10000)
self._timeline.end_time = TimeSpan(value.duration)
self._timeline.min_seek_time = TimeSpan(0)
self._timeline.max_seek_time = TimeSpan(value.duration * 10000)
self._timeline.max_seek_time = TimeSpan(value.duration)
self._controls.update_timeline_properties(self._timeline)

def set_tracklist_property(self, name: TrackListPropertyName, value: Any):
Expand Down

0 comments on commit d3a3289

Please sign in to comment.