openCom-Companion/sbapp/plyer/platforms/linux/audio.py

72 lines
1.9 KiB
Python
Raw Normal View History

2024-06-02 18:31:58 +02:00
import time
import threading
2024-06-04 04:46:47 +02:00
import RNS
2024-06-02 18:31:58 +02:00
from sbapp.plyer.facades.audio import Audio
2024-06-04 04:46:47 +02:00
from ffpyplayer.player import MediaPlayer
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
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-04 04:46:47 +02:00
run = True
while run and self.sound != None and not self.sound.get_pause():
2024-06-02 18:31:58 +02:00
time.sleep(0.25)
2024-06-04 04:46:47 +02:00
if self.duration:
pts = self.sound.get_pts()
if pts > self.duration:
run = False
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-04 04:46:47 +02:00
if self.sound != None:
self.sound.set_pause(True)
self.sound.seek(0, relative=False)
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-04 04:46:47 +02:00
self.sound = MediaPlayer(self._file_path)
self.metadata = self.sound.get_metadata()
self.duration = self.metadata["duration"]
if self.duration == None:
time.sleep(0.15)
self.metadata = self.sound.get_metadata()
self.duration = self.metadata["duration"]
2024-06-04 04:46:47 +02:00
RNS.log(str(self.metadata))
self._loaded_path = self._file_path
2024-06-02 18:31:58 +02:00
self.is_playing = True
self._check_thread = threading.Thread(target=self._check_playback, daemon=True)
self._check_thread.start()
def reload(self):
self._loaded_path = 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()