From a01eabea91f43436ecb7f0c2021514b32669b0b3 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Thu, 25 Apr 2019 10:18:13 -0500 Subject: [PATCH] Separate playback of voice messages from regular audio playback --- src/controller/buffers.py | 3 +-- src/presenters/player.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/controller/buffers.py b/src/controller/buffers.py index d80d7cc..a21dc00 100644 --- a/src/controller/buffers.py +++ b/src/controller/buffers.py @@ -1213,8 +1213,7 @@ class chatBuffer(baseBuffer): a = presenters.displayAudioPresenter(session=self.session, postObject=[attachment["audio"]], interactor=interactors.displayAudioInteractor(), view=views.displayAudio()) elif attachment["type"] == "audio_message": link = attachment["audio_message"]["link_mp3"] - output.speak(_("Playing...")) - pub.sendMessage("play", object=dict(url=link), set_info=False) + pub.sendMessage("play-message", message_url=link) elif attachment["type"] == "link": output.speak(_("Opening URL..."), True) webbrowser.open_new_tab(attachment["link"]["url"]) diff --git a/src/presenters/player.py b/src/presenters/player.py index 3688f04..9dd4ab1 100644 --- a/src/presenters/player.py +++ b/src/presenters/player.py @@ -33,6 +33,7 @@ class audioPlayer(object): self.is_playing = False # This will be the URLStream handler self.stream = None + self.message = None 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. self.is_working = False @@ -51,6 +52,7 @@ class audioPlayer(object): bassconfig["net_timeout"] = 30000 # subscribe all pubsub events. pub.subscribe(self.play, "play") + pub.subscribe(self.play_message, "play-message") pub.subscribe(self.play_all, "play-all") pub.subscribe(self.pause, "pause") pub.subscribe(self.stop, "stop") @@ -107,6 +109,19 @@ class audioPlayer(object): self.stopped = 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): """ Stop audio playback. """ if self.stream != None and self.stream.is_playing == True: @@ -114,6 +129,11 @@ class audioPlayer(object): self.stopped = True 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): """ 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: