2024-06-02 18:31:58 +02:00
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
from sbapp.plyer.facades.audio import Audio
|
2024-06-03 13:20:41 +02:00
|
|
|
from kivy.core.audio import SoundLoader
|
2024-06-02 18:31:58 +02:00
|
|
|
|
|
|
|
class LinuxAudio(Audio):
|
|
|
|
|
|
|
|
def __init__(self, file_path=None):
|
|
|
|
default_path = None
|
|
|
|
super().__init__(file_path or default_path)
|
|
|
|
|
|
|
|
self._recorder = None
|
|
|
|
self._player = None
|
|
|
|
self._check_thread = None
|
|
|
|
self._finished_callback = None
|
2024-06-03 13:20:41 +02:00
|
|
|
self._loaded_path = None
|
|
|
|
self.sound = None
|
2024-06-04 02:55:13 +02:00
|
|
|
self.is_playing = False
|
2024-06-02 18:31:58 +02:00
|
|
|
|
|
|
|
def _check_playback(self):
|
2024-06-03 13:20:41 +02:00
|
|
|
while self.sound != None and self.sound.state == "play":
|
2024-06-02 18:31:58 +02:00
|
|
|
time.sleep(0.25)
|
2024-06-04 02:55:13 +02:00
|
|
|
|
|
|
|
self.is_playing = False
|
2024-06-02 18:31:58 +02:00
|
|
|
|
|
|
|
if self._finished_callback and callable(self._finished_callback):
|
|
|
|
self._check_thread = None
|
|
|
|
self._finished_callback(self)
|
|
|
|
|
|
|
|
def _start(self):
|
|
|
|
# TODO: Implement recording
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _stop(self):
|
2024-06-03 13:20:41 +02:00
|
|
|
if self.sound != None and self.sound.state == "play":
|
|
|
|
self.sound.stop()
|
2024-06-04 02:55:13 +02:00
|
|
|
self.is_playing = False
|
2024-06-02 18:31:58 +02:00
|
|
|
|
|
|
|
def _play(self):
|
2024-06-03 13:20:41 +02:00
|
|
|
if self.sound == None or self._loaded_path != self._file_path:
|
|
|
|
self.sound = SoundLoader.load(self._file_path)
|
|
|
|
|
2024-06-02 18:31:58 +02:00
|
|
|
self.is_playing = True
|
2024-06-03 13:20:41 +02:00
|
|
|
self.sound.play()
|
2024-06-02 18:31:58 +02:00
|
|
|
|
|
|
|
self._check_thread = threading.Thread(target=self._check_playback, daemon=True)
|
|
|
|
self._check_thread.start()
|
|
|
|
|
2024-06-03 13:20:41 +02:00
|
|
|
def reload(self):
|
|
|
|
self._loaded_path = None
|
|
|
|
self.sound = None
|
2024-06-02 18:31:58 +02:00
|
|
|
|
2024-06-04 02:55:13 +02:00
|
|
|
def playing(self):
|
|
|
|
return self.is_playing
|
|
|
|
|
2024-06-02 18:31:58 +02:00
|
|
|
|
|
|
|
def instance():
|
|
|
|
return LinuxAudio()
|