Separate playback of voice messages from regular audio playback

This commit is contained in:
Manuel Cortez 2019-04-25 10:18:13 -05:00
parent 605f0da751
commit a01eabea91
2 changed files with 21 additions and 2 deletions

View File

@ -1213,8 +1213,7 @@ class chatBuffer(baseBuffer):
a = presenters.displayAudioPresenter(session=self.session, postObject=[attachment["audio"]], interactor=interactors.displayAudioInteractor(), view=views.displayAudio()) a = presenters.displayAudioPresenter(session=self.session, postObject=[attachment["audio"]], interactor=interactors.displayAudioInteractor(), view=views.displayAudio())
elif attachment["type"] == "audio_message": elif attachment["type"] == "audio_message":
link = attachment["audio_message"]["link_mp3"] link = attachment["audio_message"]["link_mp3"]
output.speak(_("Playing...")) pub.sendMessage("play-message", message_url=link)
pub.sendMessage("play", object=dict(url=link), set_info=False)
elif attachment["type"] == "link": elif attachment["type"] == "link":
output.speak(_("Opening URL..."), True) output.speak(_("Opening URL..."), True)
webbrowser.open_new_tab(attachment["link"]["url"]) webbrowser.open_new_tab(attachment["link"]["url"])

View File

@ -33,6 +33,7 @@ class audioPlayer(object):
self.is_playing = False self.is_playing = False
# This will be the URLStream handler # This will be the URLStream handler
self.stream = None self.stream = None
self.message = None
self.vol = config.app["sound"]["volume"] self.vol = config.app["sound"]["volume"]
# this variable is set to true when the URLPlayer is decoding something, thus it will block other calls to the play method. # this variable is set to true when the URLPlayer is decoding something, thus it will block other calls to the play method.
self.is_working = False self.is_working = False
@ -51,6 +52,7 @@ class audioPlayer(object):
bassconfig["net_timeout"] = 30000 bassconfig["net_timeout"] = 30000
# subscribe all pubsub events. # subscribe all pubsub events.
pub.subscribe(self.play, "play") pub.subscribe(self.play, "play")
pub.subscribe(self.play_message, "play-message")
pub.subscribe(self.play_all, "play-all") pub.subscribe(self.play_all, "play-all")
pub.subscribe(self.pause, "pause") pub.subscribe(self.pause, "pause")
pub.subscribe(self.stop, "stop") pub.subscribe(self.stop, "stop")
@ -107,6 +109,19 @@ class audioPlayer(object):
self.stopped = False self.stopped = False
self.is_working = False self.is_working = False
def play_message(self, message_url):
if self.message != None and (self.message.is_playing == True or self.message.is_stalled == True):
return self.stop_message()
output.speak(_("Playing..."))
url_ = utils.transform_audio_url(message_url)
url_ = bytes(url_, "utf-8")
try:
self.message = URLStream(url=url_)
except:
log.error("Unable to play URL %s" % (url_))
return
self.message.play()
def stop(self): def stop(self):
""" Stop audio playback. """ """ Stop audio playback. """
if self.stream != None and self.stream.is_playing == True: if self.stream != None and self.stream.is_playing == True:
@ -114,6 +129,11 @@ class audioPlayer(object):
self.stopped = True self.stopped = True
self.queue = [] self.queue = []
def stop_message(self):
if hasattr(self, "message") and self.message != None and self.message.is_playing == True:
self.message.stop()
self.message = None
def pause(self): def pause(self):
""" pause the current playback, without destroying the queue or the current stream. If the stream is already paused this function will resume the playback. """ """ pause the current playback, without destroying the queue or the current stream. If the stream is already paused this function will resume the playback. """
if self.stream != None: if self.stream != None: