From 768943e166e6676f5451e56bdf5cb117354edf68 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Mon, 3 Jun 2024 13:20:41 +0200 Subject: [PATCH] Unify audio playback for Linux and Android --- sbapp/main.py | 51 ++++++++++++-------------- sbapp/plyer/platforms/android/audio.py | 4 +- sbapp/plyer/platforms/linux/audio.py | 23 +++++++----- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/sbapp/main.py b/sbapp/main.py index 9ecd6e2..28ac789 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -1255,7 +1255,7 @@ class SidebandApp(MDApp): def cb(dt): self.messages_view.ids.message_send_button.disabled = False Clock.schedule_once(cb, 0.5) - + if self.root.ids.screen_manager.current == "messages_screen": if self.outbound_mode_propagation and self.sideband.message_router.get_outbound_propagation_node() == None: self.messages_view.send_error_dialog = MDDialog( @@ -1516,34 +1516,31 @@ class SidebandApp(MDApp): else: return False - temp_path = self.sideband.rec_cache+"/msg."+audio_type + temp_path = self.sideband.rec_cache+"/msg."+audio_type + if self.last_msg_audio != audio_field[1]: + self.last_msg_audio = audio_field[1] - if audio_type == "ogg": - if self.last_msg_audio != audio_field[1]: - self.last_msg_audio = audio_field[1] - + if audio_type == "ogg": + # No decoding necessary for OGG/OPUS with open(temp_path, "wb") as af: af.write(self.last_msg_audio) - - if not RNS.vendor.platformutils.is_android(): - self.msg_sound = SoundLoader.load(temp_path) - - if RNS.vendor.platformutils.is_android(): - if self.msg_sound != None and self.msg_sound._player != None and self.msg_sound._player.isPlaying(): - self.msg_sound.stop() - else: - from plyer import audio - self.msg_sound = audio - self.msg_sound._file_path = temp_path - self.msg_sound.play() - + else: - if self.msg_sound != None and self.msg_sound.state == "play": - self.msg_sound.stop() - return True - else: - self.msg_sound.play() - return True + raise NotImplementedError(audio_type) + + if self.msg_sound == None: + from plyer import audio + self.msg_sound = audio + + self.msg_sound._file_path = temp_path + self.msg_sound.reload() + + if self.msg_sound != None and self.msg_sound._player != None and self.msg_sound._player.isPlaying(): + self.msg_sound.stop() + else: + + self.msg_sound.play() + def message_record_audio_action(self): ss = int(dp(18)) @@ -1725,8 +1722,8 @@ class SidebandApp(MDApp): DialogItem(IconLeftWidget(icon="microphone-message"), text="[size="+str(ss)+"]Audio Recording[/size]", on_release=a_audio_hq), DialogItem(IconLeftWidget(icon="file-outline"), text="[size="+str(ss)+"]File Attachment[/size]", on_release=a_file)] - if RNS.vendor.platformutils.is_linux(): - ad_items.pop(3) + # if RNS.vendor.platformutils.is_linux(): + # ad_items.pop(3) self.attach_dialog = MDDialog( title="Add Attachment", diff --git a/sbapp/plyer/platforms/android/audio.py b/sbapp/plyer/platforms/android/audio.py index 33739b8..b6421b4 100644 --- a/sbapp/plyer/platforms/android/audio.py +++ b/sbapp/plyer/platforms/android/audio.py @@ -42,7 +42,6 @@ class AndroidAudio(Audio): def _start(self): self._recorder = MediaRecorder() - # AAC Format, decent quality if self._format == "aac": self._recorder.setAudioSource(AudioSource.DEFAULT) self._recorder.setAudioSamplingRate(48000) @@ -52,7 +51,6 @@ class AndroidAudio(Audio): self._recorder.setAudioEncoder(AudioEncoder.AAC) else: - # OPUS self._recorder.setAudioSource(AudioSource.DEFAULT) self._recorder.setAudioSamplingRate(48000) self._recorder.setAudioEncodingBitRate(128000) @@ -85,6 +83,8 @@ class AndroidAudio(Audio): self._check_thread = threading.Thread(target=self._check_playback, daemon=True) self._check_thread.start() + def reload(self): + self._stop() def instance(): diff --git a/sbapp/plyer/platforms/linux/audio.py b/sbapp/plyer/platforms/linux/audio.py index 55cdb7c..2cf3ec3 100644 --- a/sbapp/plyer/platforms/linux/audio.py +++ b/sbapp/plyer/platforms/linux/audio.py @@ -1,6 +1,7 @@ import time import threading from sbapp.plyer.facades.audio import Audio +from kivy.core.audio import SoundLoader class LinuxAudio(Audio): @@ -12,9 +13,11 @@ class LinuxAudio(Audio): self._player = None self._check_thread = None self._finished_callback = None + self._loaded_path = None + self.sound = None def _check_playback(self): - while self.is_playing: + while self.sound != None and self.sound.state == "play": time.sleep(0.25) if self._finished_callback and callable(self._finished_callback): @@ -26,22 +29,22 @@ class LinuxAudio(Audio): pass def _stop(self): - # TODO: Implement recording - pass + if self.sound != None and self.sound.state == "play": + self.sound.stop() def _play(self): - # TODO: Implement playback + if self.sound == None or self._loaded_path != self._file_path: + self.sound = SoundLoader.load(self._file_path) + self.is_playing = True + self.sound.play() self._check_thread = threading.Thread(target=self._check_playback, daemon=True) self._check_thread.start() - def fauxplay(): - time.sleep(1.5) - self.is_playing = False - - threading.Thread(target=fauxplay, daemon=True).start() - + def reload(self): + self._loaded_path = None + self.sound = None def instance():