Source code for arcade.sound

"""
Sound library.
From https://github.com/TaylorSMarks/playsound/blob/master/playsound.py
"""

from platform import system
import typing
import pyglet

[docs]class Sound: def __init__(self, file_name: str): self.file_name = file_name self.player = pyglet.media.load(file_name)
[docs] def play(self): if self.player.is_queued: player = pyglet.media.load(self.file_name) player.play() else: self.player.play()
[docs]class PlaysoundException(Exception): pass
def _load_sound_library(): """ Special code for Windows so we grab the proper avbin from our directory. Otherwise hope the correct package is installed. """ # lazy loading if not _load_sound_library._sound_library_loaded: _load_sound_library._sound_library_loaded = True else: return import pyglet_ffmpeg pyglet_ffmpeg.load_ffmpeg() # Initialize static function variable _load_sound_library._sound_library_loaded = False
[docs]def load_sound(file_name: str): """ Load a sound """ sound = Sound(file_name) return sound
[docs]def play_sound(sound): """ Play a sound """ sound.play()
[docs]def stop_sound(sound: pyglet.media.Source): sound.pause()
_load_sound_library()