-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.py
54 lines (46 loc) · 1.27 KB
/
Sound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Sound.py
MIT License (c) Faure Systems <dev at faure dot systems>
Sound play audio with aplay (until it ends).
'''
import os, subprocess
class Sound:
#__________________________________________________________________
def __init__(self, logger=None):
super().__init__()
self._logger = logger
self._file = None
self._player = None
#__________________________________________________________________
def play(self, file):
if self.isPlaying():
return
if os.path.exists(file):
self._file= file
try:
self._player = subprocess.Popen(
['aplay', file],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except Exception as e:
if self._logger:
self._logger.error("Sound API : failed to load file")
self._logger.debug(e)
if self._logger:
self._logger.info("{} {}".format("Sound API : playing", file))
else:
self._file= None
if self._logger:
self._logger.info("{} {}".format("Sound API : file not found", file))
#__________________________________________________________________
def isPlaying(self):
if self._player is not None:
self._player.poll()
if self._player.returncode is None:
return True
else:
return False
else:
return False