2022-04-16 14:37:28 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-03-20 14:00:35 -05:00
|
|
|
import subprocess
|
|
|
|
import threading
|
|
|
|
import platform
|
|
|
|
|
2021-01-23 11:02:02 -08:00
|
|
|
from manimlib.utils.directories import get_sound_dir
|
2022-04-12 19:19:59 +08:00
|
|
|
from manimlib.utils.file_ops import find_file
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2022-04-16 14:37:28 +08:00
|
|
|
def get_full_sound_file_path(sound_file_name: str) -> str:
|
2021-01-06 10:39:34 -08:00
|
|
|
return find_file(
|
2019-01-24 22:24:01 -08:00
|
|
|
sound_file_name,
|
2021-01-23 11:02:02 -08:00
|
|
|
directories=[get_sound_dir()],
|
2021-07-28 23:13:15 +08:00
|
|
|
extensions=[".wav", ".mp3", ""]
|
2019-01-24 22:24:01 -08:00
|
|
|
)
|
2025-03-20 14:00:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
def play_sound(sound_file):
|
|
|
|
"""Play a sound file using the system's audio player"""
|
|
|
|
full_path = get_full_sound_file_path(sound_file)
|
|
|
|
system = platform.system()
|
|
|
|
|
|
|
|
if system == "Windows":
|
|
|
|
# Windows
|
|
|
|
subprocess.Popen(
|
|
|
|
["powershell", "-c", f"(New-Object Media.SoundPlayer '{full_path}').PlaySync()"],
|
|
|
|
shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
|
|
|
)
|
|
|
|
elif system == "Darwin":
|
|
|
|
# macOS
|
|
|
|
subprocess.Popen(
|
|
|
|
["afplay", full_path],
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
subprocess.Popen(
|
|
|
|
["aplay", full_path],
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
|
|
|
)
|