Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2415 from forslund/feature/end-of-playlist-message
Browse files Browse the repository at this point in the history
Send mycroft.audio.queue_end
  • Loading branch information
forslund authored Jan 15, 2020
2 parents 1faedf6 + fffa6cc commit 689d072
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
17 changes: 12 additions & 5 deletions mycroft/audio/audioservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,19 @@ def load_services_callback(self):
self.bus.on('recognizer_loop:record_end', self._restore_volume)

def track_start(self, track):
"""Callback method called from the services to indicate start of
playback of a track or end of playlist.
"""
Callback method called from the services to indicate start of
playback of a track.
"""
self.bus.emit(Message('mycroft.audio.playing_track',
data={'track': track}))
if track:
# Inform about the track about to start.
LOG.debug('New track coming up!')
self.bus.emit(Message('mycroft.audio.playing_track',
data={'track': track}))
else:
# If no track is about to start last track of the queue has been
# played.
LOG.debug('End of playlist!')
self.bus.emit(Message('mycroft.audio.queue_end'))

def _pause(self, message=None):
"""
Expand Down
1 change: 1 addition & 0 deletions mycroft/audio/services/simple/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def _play(self, message):
self.bus.emit(Message('SimpleAudioServicePlay',
{'repeat': repeat}))
else:
self._track_start_callback(None)
self._is_playing = False
self._paused = False

Expand Down
8 changes: 8 additions & 0 deletions mycroft/audio/services/vlc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def __init__(self, config, bus=None, name='vlc'):
self.track_list = self.instance.media_list_new()
self.list_player.set_media_list(self.track_list)
self.vlc_events = self.player.event_manager()
self.vlc_list_events = self.list_player.event_manager()
self.vlc_events.event_attach(vlc.EventType.MediaPlayerPlaying,
self.track_start, 1)
self.vlc_list_events.event_attach(vlc.EventType.MediaListPlayerPlayed,
self.queue_ended, 0)
self.config = config
self.bus = bus
self.name = name
Expand All @@ -40,6 +43,11 @@ def track_start(self, data, other):
if self._track_start_callback:
self._track_start_callback(self.track_info()['name'])

def queue_ended(self, data, other):
LOG.debug('Queue ended')
if self._track_start_callback:
self._track_start_callback(None)

def supported_uris(self):
return ['file', 'http', 'https']

Expand Down
19 changes: 19 additions & 0 deletions test/unittests/audio/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ def test_audio_backend_shutdown(self, mock_load_services):
self.assertTrue(backend.shutdown.called)
self.assertTrue(second_backend.shutdown.called)

@mock.patch('mycroft.audio.audioservice.load_services')
def test_audio_service_track_start(self, mock_load_services):
"""Test start of new track messages."""
backend, second_backend = setup_mock_backends(mock_load_services,
self.emitter)
service = audio_service.AudioService(self.emitter)
service.load_services_callback()
service.default = backend

self.emitter.reset()
service.track_start('The universe song')
service.track_start(None)
self.assertEqual(self.emitter.types, ['mycroft.audio.playing_track',
'mycroft.audio.queue_end'])
self.assertEqual(self.emitter.results,
[{'track': 'The universe song'}, {}])

service.shutdown()

@mock.patch('mycroft.audio.audioservice.load_services')
def test_audio_service_methods_not_playing(self, mock_load_services):
"""Check that backend methods aren't called when stopped."""
Expand Down

0 comments on commit 689d072

Please sign in to comment.