-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPlayer.py
302 lines (252 loc) · 9.06 KB
/
Player.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# -*- coding: utf-8 -*-
import asyncio
import subprocess
import os
import platform
import queue
import signal
import threading
from robot import logging
from ctypes import CFUNCTYPE, c_char_p, c_int, cdll
from contextlib import contextmanager
from . import utils
logger = logging.getLogger(__name__)
def py_error_handler(filename, line, function, err, fmt):
pass
ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)
@contextmanager
def no_alsa_error():
try:
asound = cdll.LoadLibrary("libasound.so")
asound.snd_lib_error_set_handler(c_error_handler)
yield
asound.snd_lib_error_set_handler(None)
except:
yield
pass
def play(fname, onCompleted=None):
player = getPlayerByFileName(fname)
player.play(fname, onCompleted=onCompleted)
def getPlayerByFileName(fname):
foo, ext = os.path.splitext(fname)
if ext in [".mp3", ".wav"]:
return SoxPlayer()
class AbstractPlayer(object):
def __init__(self, **kwargs):
super(AbstractPlayer, self).__init__()
def play(self):
pass
def play_block(self):
pass
def stop(self):
pass
def is_playing(self):
return False
def join(self):
pass
class SoxPlayer(AbstractPlayer):
SLUG = "SoxPlayer"
def __init__(self, **kwargs):
super(SoxPlayer, self).__init__(**kwargs)
self.playing = False
self.proc = None
self.delete = False
self.onCompleteds = []
# 创建一个锁用于保证同一时间只有一个音频在播放
self.play_lock = threading.Lock()
self.play_queue = queue.Queue() # 播放队列
self.consumer_thread = threading.Thread(target=self.playLoop)
self.consumer_thread.start()
self.loop = asyncio.new_event_loop() # 创建事件循环
self.thread_loop = threading.Thread(target=self.loop.run_forever)
self.thread_loop.start()
def executeOnCompleted(self, res, onCompleted):
# 全部播放完成,播放统一的 onCompleted()
res and onCompleted and onCompleted()
if self.play_queue.empty():
for onCompleted in self.onCompleteds:
onCompleted and onCompleted()
def playLoop(self):
while True:
(src, onCompleted) = self.play_queue.get()
if src:
with self.play_lock:
logger.info(f"开始播放音频:{src}")
self.src = src
res = self.doPlay(src)
self.play_queue.task_done()
# 将 onCompleted() 方法的调用放到事件循环的线程中执行
self.loop.call_soon_threadsafe(
self.executeOnCompleted, res, onCompleted
)
def doPlay(self, src):
system = platform.system()
if system == "Darwin":
cmd = ["afplay", str(src)]
else:
cmd = ["play", str(src)]
logger.debug("Executing %s", " ".join(cmd))
self.proc = subprocess.Popen(
cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
self.playing = True
self.proc.wait()
self.playing = False
if self.delete:
utils.check_and_delete(src)
logger.info(f"播放完成:{src}")
return self.proc and self.proc.returncode == 0
def play(self, src, delete=False, onCompleted=None):
if src and (os.path.exists(src) or src.startswith("http")):
self.delete = delete
self.play_queue.put((src, onCompleted))
else:
logger.critical(f"path not exists: {src}", stack_info=True)
def preappendCompleted(self, onCompleted):
onCompleted and self.onCompleteds.insert(0, onCompleted)
def appendOnCompleted(self, onCompleted):
onCompleted and self.onCompleteds.append(onCompleted)
def play_block(self):
self.run()
def stop(self):
if self.proc:
self.onCompleteds = []
self.proc.terminate()
self.proc.kill()
self.proc = None
self.playing = False
self._clear_queue()
if self.delete:
utils.check_and_delete(self.src)
def is_playing(self):
return self.playing or not self.play_queue.empty()
def join(self):
self.play_queue.join()
def _clear_queue(self):
with self.play_queue.mutex:
self.play_queue.queue.clear()
class MusicPlayer(SoxPlayer):
"""
给音乐播放器插件使用的,
在 SOXPlayer 的基础上增加了列表的支持,
并支持暂停和恢复播放
"""
SLUG = "MusicPlayer"
def __init__(self, playlist, plugin, **kwargs):
super(MusicPlayer, self).__init__(**kwargs)
self.playlist = playlist
self.plugin = plugin
self.idx = 0
self.pausing = False
def update_playlist(self, playlist):
super().stop()
self.playlist = playlist
self.idx = 0
self.play()
def play(self):
logger.debug("MusicPlayer play")
path = self.playlist[self.idx]
super().stop()
super().play(path, False, self.next)
def next(self):
logger.debug("MusicPlayer next")
super().stop()
self.idx = (self.idx + 1) % len(self.playlist)
self.play()
def prev(self):
logger.debug("MusicPlayer prev")
super().stop()
self.idx = (self.idx - 1) % len(self.playlist)
self.play()
def pause(self):
logger.debug("MusicPlayer pause")
self.pausing = True
if self.proc:
os.kill(self.proc.pid, signal.SIGSTOP)
def stop(self):
if self.proc:
logger.debug(f"MusicPlayer stop {self.proc.pid}")
self.onCompleteds = []
os.kill(self.proc.pid, signal.SIGSTOP)
self.proc.terminate()
self.proc.kill()
self.proc = None
def resume(self):
logger.debug("MusicPlayer resume")
self.pausing = False
self.onCompleteds = [self.next]
if self.proc:
os.kill(self.proc.pid, signal.SIGCONT)
def is_playing(self):
return self.playing
def is_pausing(self):
return self.pausing
def turnUp(self):
system = platform.system()
if system == "Darwin":
res = subprocess.run(
["osascript", "-e", "output volume of (get volume settings)"],
shell=False,
capture_output=True,
universal_newlines=True,
)
volume = int(res.stdout.strip())
volume += 20
if volume >= 100:
volume = 100
self.plugin.say("音量已经最大啦")
subprocess.run(["osascript", "-e", f"set volume output volume {volume}"])
elif system == "Linux":
res = subprocess.run(
["amixer sget Master | grep 'Mono:' | awk -F'[][]' '{ print $2 }'"],
shell=True,
capture_output=True,
universal_newlines=True,
)
if res.stdout != "" and res.stdout.strip().endswith("%"):
volume = int(res.stdout.strip().replace("%", ""))
volume += 20
if volume >= 100:
volume = 100
self.plugin.say("音量已经最大啦")
subprocess.run(["amixer", "set", "Master", f"{volume}%"])
else:
subprocess.run(["amixer", "set", "Master", "20%+"])
else:
self.plugin.say("当前系统不支持调节音量")
self.resume()
def turnDown(self):
system = platform.system()
if system == "Darwin":
res = subprocess.run(
["osascript", "-e", "output volume of (get volume settings)"],
shell=False,
capture_output=True,
universal_newlines=True,
)
volume = int(res.stdout.strip())
volume -= 20
if volume <= 20:
volume = 20
self.plugin.say("音量已经很小啦")
subprocess.run(["osascript", "-e", f"set volume output volume {volume}"])
elif system == "Linux":
res = subprocess.run(
["amixer sget Master | grep 'Mono:' | awk -F'[][]' '{ print $2 }'"],
shell=True,
capture_output=True,
universal_newlines=True,
)
if res.stdout != "" and res.stdout.endswith("%"):
volume = int(res.stdout.replace("%", "").strip())
volume -= 20
if volume <= 20:
volume = 20
self.plugin.say("音量已经最小啦")
subprocess.run(["amixer", "set", "Master", f"{volume}%"])
else:
subprocess.run(["amixer", "set", "Master", "20%-"])
else:
self.plugin.say("当前系统不支持调节音量")
self.resume()