Unify audio playback for Linux and Android

This commit is contained in:
Mark Qvist 2024-06-03 13:20:41 +02:00
parent 4b6ff73907
commit 768943e166
3 changed files with 39 additions and 39 deletions

View File

@ -1255,7 +1255,7 @@ class SidebandApp(MDApp):
def cb(dt): def cb(dt):
self.messages_view.ids.message_send_button.disabled = False self.messages_view.ids.message_send_button.disabled = False
Clock.schedule_once(cb, 0.5) Clock.schedule_once(cb, 0.5)
if self.root.ids.screen_manager.current == "messages_screen": if self.root.ids.screen_manager.current == "messages_screen":
if self.outbound_mode_propagation and self.sideband.message_router.get_outbound_propagation_node() == None: if self.outbound_mode_propagation and self.sideband.message_router.get_outbound_propagation_node() == None:
self.messages_view.send_error_dialog = MDDialog( self.messages_view.send_error_dialog = MDDialog(
@ -1516,34 +1516,31 @@ class SidebandApp(MDApp):
else: else:
return False 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 audio_type == "ogg":
if self.last_msg_audio != audio_field[1]: # No decoding necessary for OGG/OPUS
self.last_msg_audio = audio_field[1]
with open(temp_path, "wb") as af: with open(temp_path, "wb") as af:
af.write(self.last_msg_audio) 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: else:
if self.msg_sound != None and self.msg_sound.state == "play": raise NotImplementedError(audio_type)
self.msg_sound.stop()
return True if self.msg_sound == None:
else: from plyer import audio
self.msg_sound.play() self.msg_sound = audio
return True
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): def message_record_audio_action(self):
ss = int(dp(18)) 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="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)] DialogItem(IconLeftWidget(icon="file-outline"), text="[size="+str(ss)+"]File Attachment[/size]", on_release=a_file)]
if RNS.vendor.platformutils.is_linux(): # if RNS.vendor.platformutils.is_linux():
ad_items.pop(3) # ad_items.pop(3)
self.attach_dialog = MDDialog( self.attach_dialog = MDDialog(
title="Add Attachment", title="Add Attachment",

View File

@ -42,7 +42,6 @@ class AndroidAudio(Audio):
def _start(self): def _start(self):
self._recorder = MediaRecorder() self._recorder = MediaRecorder()
# AAC Format, decent quality
if self._format == "aac": if self._format == "aac":
self._recorder.setAudioSource(AudioSource.DEFAULT) self._recorder.setAudioSource(AudioSource.DEFAULT)
self._recorder.setAudioSamplingRate(48000) self._recorder.setAudioSamplingRate(48000)
@ -52,7 +51,6 @@ class AndroidAudio(Audio):
self._recorder.setAudioEncoder(AudioEncoder.AAC) self._recorder.setAudioEncoder(AudioEncoder.AAC)
else: else:
# OPUS
self._recorder.setAudioSource(AudioSource.DEFAULT) self._recorder.setAudioSource(AudioSource.DEFAULT)
self._recorder.setAudioSamplingRate(48000) self._recorder.setAudioSamplingRate(48000)
self._recorder.setAudioEncodingBitRate(128000) 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 = threading.Thread(target=self._check_playback, daemon=True)
self._check_thread.start() self._check_thread.start()
def reload(self):
self._stop()
def instance(): def instance():

View File

@ -1,6 +1,7 @@
import time import time
import threading import threading
from sbapp.plyer.facades.audio import Audio from sbapp.plyer.facades.audio import Audio
from kivy.core.audio import SoundLoader
class LinuxAudio(Audio): class LinuxAudio(Audio):
@ -12,9 +13,11 @@ class LinuxAudio(Audio):
self._player = None self._player = None
self._check_thread = None self._check_thread = None
self._finished_callback = None self._finished_callback = None
self._loaded_path = None
self.sound = None
def _check_playback(self): def _check_playback(self):
while self.is_playing: while self.sound != None and self.sound.state == "play":
time.sleep(0.25) time.sleep(0.25)
if self._finished_callback and callable(self._finished_callback): if self._finished_callback and callable(self._finished_callback):
@ -26,22 +29,22 @@ class LinuxAudio(Audio):
pass pass
def _stop(self): def _stop(self):
# TODO: Implement recording if self.sound != None and self.sound.state == "play":
pass self.sound.stop()
def _play(self): 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.is_playing = True
self.sound.play()
self._check_thread = threading.Thread(target=self._check_playback, daemon=True) self._check_thread = threading.Thread(target=self._check_playback, daemon=True)
self._check_thread.start() self._check_thread.start()
def fauxplay(): def reload(self):
time.sleep(1.5) self._loaded_path = None
self.is_playing = False self.sound = None
threading.Thread(target=fauxplay, daemon=True).start()
def instance(): def instance():