Catch simpleaudio playback errors

Because of course sound isn't just gonna work on linux and
especially flatpak. Make sure to not spawn an error popup every time
we try to play a notification. Maybe use mpv in the future for
SFX playback (when we'll have a media player implemented),
since it has support for all these backends out of the box.
This commit is contained in:
miruka 2021-03-01 09:45:28 -04:00
parent cfce4e1ddc
commit f0d7757f0c

View File

@ -149,6 +149,7 @@ class Backend:
self.notification_avatar_cache: Dict[str, Path] = {} # {mxc: path}
self.notifications_working: bool = True
self.audio_working: bool = True
def __repr__(self) -> str:
@ -600,7 +601,16 @@ class Backend:
try:
content = pyotherside.qrc_get_file_contents(path)
except ValueError:
simpleaudio.WaveObject.from_wave_file(path).play()
sa = simpleaudio.WaveObject.from_wave_file(path)
else:
wave_read = wave.open(io.BytesIO(content))
simpleaudio.WaveObject.from_wave_read(wave_read).play()
sa = simpleaudio.WaveObject.from_wave_read(wave_read)
try:
sa.play()
self.audio_working = True
except Exception as e: # noqa
if self.audio_working:
trace = traceback.format_exc().rstrip()
log.error("Playing audio failed\n%s", trace)
self.audio_working = False