From b7721d89a426df905cb311aea81b05aa3336041c Mon Sep 17 00:00:00 2001 From: Philip Trauner Date: Wed, 7 Dec 2016 23:33:26 +0100 Subject: [PATCH] Added play_sound --- Shared/Utils.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Shared/Utils.py b/Shared/Utils.py index 38d08c8..bae4483 100644 --- a/Shared/Utils.py +++ b/Shared/Utils.py @@ -1,14 +1,24 @@ from traceback import print_exception -from sys import exc_info +from sys import exc_info, byteorder import platform import os import struct import socket import fcntl +import subprocess class HostnameNotChangedError(PermissionError): def __init__(self): - super(PermissionError, self).__init__("hostname could not be changed") + super(PermissionError, self).__init__("hostname could not be changed") + +class NotSupportedOnPlatform(OSError): + def __init__(self): + super(OSError, self).__init__("feature not avaliable on OS") + +class PlaybackFailure(OSError): + def __init__(self): + super(OSError, self).__init__("audio playback failed") + def capture_trace(): exc_type, exc_value, exc_traceback = exc_info() @@ -70,3 +80,13 @@ def get_ip_address(ifname=None): ip_address = sock.getsockname()[0] sock.close() return ip_address + + +def play_sound(path): + if is_linux() or is_darwin(): + try: + subprocess.check_call(["aplay" if is_linux() else "afplay", path]) + except subprocess.CalledProcessError as e: + raise PlaybackFailure() + else: + raise NotSupportedOnPlatform() \ No newline at end of file