Bugfix/simple audioservice (#1774)

* Use correct method to guess mime type

* Use the commands from mycroft.conf

- Remove hardcoded commands and use the ones from mycroft.util to handle
configurations.
- Improve error handling
pull/1775/head
Åke 2018-08-29 21:25:53 +02:00 committed by Steve Penrod
parent 5c42ce8eae
commit bd7cd7daee
1 changed files with 20 additions and 16 deletions

View File

@ -18,6 +18,7 @@ from time import sleep
from mycroft.audio.services import AudioBackend
from mycroft.messagebus.message import Message
from mycroft.util.log import LOG
from mycroft.util import play_mp3, play_ogg, play_wav
import mimetypes
from requests import Session
@ -29,7 +30,7 @@ def find_mime(path):
if 200 <= response.status_code < 300:
mime = response.headers['content-type']
if not mime:
mime = mimetypes.guess_mime(path)[0]
mime = mimetypes.guess_type(path)[0]
if mime:
return mime.split('/')
@ -83,27 +84,30 @@ class SimpleAudioService(AudioBackend):
else: # Assume string
track = self.tracks[self.index]
mime = find_mime(track)
print('MIME: ' + str(mime))
# Indicate to audio service which track is being played
if self._track_start_callback:
self._track_start_callback(track)
# Replace file:// uri's with normal paths
track = track.replace('file://', '')
proc = None
if 'mpeg' in mime[1]:
proc = 'mpg123'
elif 'ogg' in mime[1]:
proc = 'ogg123'
elif 'wav' in mime[1]:
proc = 'aplay'
else:
proc = 'mpg123' # If no mime info could be determined gues mp3
if proc:
self.process = subprocess.Popen([proc, track])
# Wait for completion or stop request
while self.process.poll() is None and not self._stop_signal:
sleep(0.25)
try:
if 'mpeg' in mime[1]:
self.process = play_mp3(track)
elif 'ogg' in mime[1]:
self.process = play_ogg(track)
elif 'wav' in mime[1]:
self.process = play_wav(track)
else:
# If no mime info could be determined guess mp3
self.process = play_mp3(track)
except FileNotFoundError as e:
LOG.error('Couldn\'t play audio, {}'.format(repr(e)))
self.process = None
# Wait for completion or stop request
while (self.process and self.process.poll() is None and
not self._stop_signal):
sleep(0.25)
if self._stop_signal:
self.process.terminate()