Source code for arcade.sound

Sound library.

from platform import system
import typing
import pyglet
from arcade.monkey_patch_pyglet import *

[docs]class Sound: def __init__(self, file_name: str): self.file_name = file_name self.player =
[docs] def play(self): if self.player.is_queued: player = else:
[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_ffmpeg2 pyglet_ffmpeg2.load_ffmpeg() # Initialize static function variable _load_sound_library._sound_library_loaded = False
[docs]def load_sound(file_name: str): """ Load a sound. Support for .wav files. If ffmpeg is available, will work with ogg and mp3 as well. :param str file_name: Name of the sound file to load. :returns: Sound object :rtype: Sound """ try: sound = Sound(file_name) return sound except Exception as e: print("Unable to load {str}.", e) return None
[docs]def play_sound(sound: Sound): """ Play a sound. :param Sound sound: Sound loaded by load_sound. Do NOT use a string here for the filename. """ if sound is None: print("Unable to play sound, no data passed in.") elif isinstance(sound, str): raise Exception("Error, passed in a string as a sound. Make sure to use load_sound first, and use that result in play_sound.") try: except Exception as e: print("Error playing sound.", e)
[docs]def stop_sound(sound: """ Stop a sound that is currently playing. :param sound: """ sound.pause()