Skip to content

Commit

Permalink
audioservice track info
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored and forslund committed Jun 18, 2020
1 parent 7ff3db5 commit 8bc56d7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
8 changes: 8 additions & 0 deletions mycroft/audio/audioservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def _next(self, message=None):
message: message bus message, not used but required
"""
if self.current:
if self.current.index + 1 > len(self.current.tracks):
self.current.index = 0
else:
self.current.index += 1
self.current.next()

def _prev(self, message=None):
Expand All @@ -248,6 +252,10 @@ def _prev(self, message=None):
message: message bus message, not used but required
"""
if self.current:
if self.current.index - 1 < 0:
self.current.index = 0
else:
self.current.index -= 1
self.current.previous()

def _stop(self, message=None):
Expand Down
21 changes: 16 additions & 5 deletions mycroft/audio/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ def __init__(self, config, bus):
self.bus = bus
self.config = config
self.index = 0
self.tracks = []
self.track_data = {}
self.bus.on('play:status', self.handle_track_status)

def handle_track_status(self, message):
index = message.data.get("playlist_position")
if index < 0:
uri = message.data.get("uri")
if index is None and uri:
for idx in self.track_data:
if self.track_data[idx].get("uri", "") == uri:
index = idx
break
elif index is None:
index = len(self.track_data) - 1
self.track_data[index] = message.data
for k in message.data:
if message.data[k]:
self.track_data[index][k] = message.data[k]

@abstractmethod
def supported_uris(self):
Expand Down Expand Up @@ -105,13 +114,15 @@ def next(self):
"""
Skip to next track in playlist.
"""
pass
# play index + 1
self.play()

def previous(self):
"""
Skip to previous track in playlist.
"""
pass
# play index - 1
self.play()

def lower_volume(self):
"""
Expand Down Expand Up @@ -151,7 +162,7 @@ def track_info(self):
Dict with track info.
"""
if self.index not in self.track_data:
return None
return {}
return self.track_data[self.index]

def shutdown(self):
Expand Down
13 changes: 0 additions & 13 deletions mycroft/audio/services/chromecast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,6 @@ def restore_volume(self):
# self.cast.volume_up()
pass

def track_info(self):
""" Return info about currently playing track. """
info = {}
ret = {}
ret['name'] = info.get('name', '')
if 'album' in info:
ret['artist'] = info['album']['artists'][0]['name']
ret['album'] = info['album'].get('name', '')
else:
ret['artist'] = ''
ret['album'] = ''
return ret

def shutdown(self):
""" Disconnect from the device. """
self.cast.disconnect()
Expand Down
10 changes: 5 additions & 5 deletions mycroft/audio/services/mopidy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def restore_volume(self):
def track_info(self):
info = self.mopidy.currently_playing()
ret = {}
ret['name'] = info.get('name', '')
if 'album' in info:
if self.index in self.track_data:
ret = self.track_data[self.index]
if "track" not in ret:
ret['track'] = info.get('name', '')
if "album" not in ret and 'album' in info:
ret['artist'] = info['album']['artists'][0]['name']
ret['album'] = info['album'].get('name', '')
else:
ret['artist'] = ''
ret['album'] = ''
return ret


Expand Down
42 changes: 22 additions & 20 deletions mycroft/audio/services/mplayer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ def play(self, repeat=False):
"""
self.stop()
if len(self.tracks):
# play first track
self.mpc.loadfile(self.tracks[0])
# add other tracks
for track in self.tracks[1:]:
self.mpc.loadfile(track, 1)
# play self.index track
self.mpc.loadfile(self.tracks[self.index])

def stop(self):
self.mpc.stop()
Expand All @@ -71,16 +68,12 @@ def resume(self):
self.mpc.pause()

def next(self):
self.index += 1
if self.index > len(self.tracks):
self.index = 0
self.play()
# just play next self.index
self.play()

def previous(self):
self.index -= 1
if self.index < 0:
self.index = 0
self.play()
# just play previous self.index
self.play()

def lower_volume(self):
if self.normal_volume is None:
Expand All @@ -102,13 +95,22 @@ def track_info(self):
Dict with track info.
"""
ret = {}
ret['title'] = self.mpc.get_meta_title()
ret['artist'] = self.mpc.get_meta_artist()
ret['album'] = self.mpc.get_meta_album()
ret['genre'] = self.mpc.get_meta_genre()
ret['year'] = self.mpc.get_meta_year()
ret['track'] = self.mpc.get_meta_track()
ret['comment'] = self.mpc.get_meta_comment()
if self.index in self.track_data:
ret = self.track_data[self.index]
if "title" not in ret:
ret['title'] = self.mpc.get_meta_title()
if "artist" not in ret:
ret['artist'] = self.mpc.get_meta_artist()
if "album" not in ret:
ret['album'] = self.mpc.get_meta_album()
if "genre" not in ret:
ret['genre'] = self.mpc.get_meta_genre()
if "year" not in ret:
ret['year'] = self.mpc.get_meta_year()
if "track" not in ret:
ret['track'] = self.mpc.get_meta_track()
if "comment" not in ret:
ret['comment'] = self.mpc.get_meta_comment()
return ret

def shutdown(self):
Expand Down
3 changes: 0 additions & 3 deletions mycroft/audio/services/vlc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,10 @@ def resume(self):
def next(self):
""" Skip to next track in playlist. """
self.list_player.next()
self.index += 1

def previous(self):
""" Skip to previous track in playlist. """
self.list_player.previous()
if self.index > 0:
self.index -= 1

def lower_volume(self):
""" Lower volume (will be called when mycroft is listening
Expand Down
7 changes: 5 additions & 2 deletions mycroft/skills/common_play_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def CPS_play(self, *args, **kwargs):
if 'utterance' not in kwargs:
kwargs['utterance'] = self.play_service_string
self.audioservice.play(*args, **kwargs)
self.CPS_send_status(uri=args[0],
status=CPSTrackStatus.PLAYING_AUDIOSERVICE)

def stop(self):
"""Stop anything playing on the audioservice."""
Expand Down Expand Up @@ -234,9 +236,9 @@ def CPS_start(self, phrase, data):
# self.CPS_play("http://zoosh.com/stream_music")
pass

def CPS_send_status(self, artist='', track='', album='', image='',
def CPS_send_status(self, uri="", artist='', track='', album='', image='',
track_length="", current_position="",
playlist_position=-1,
playlist_position=None,
status=CPSTrackStatus.DISAMBIGUATION, **kwargs):
"""Inform system of playback status.
Expand All @@ -255,6 +257,7 @@ def CPS_send_status(self, artist='', track='', album='', image='',
image (str): url for image to show
"""
data = {'skill': self.name,
"uri": uri,
'artist': artist,
'album': album,
'track': track,
Expand Down

0 comments on commit 8bc56d7

Please sign in to comment.