-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_player.py
144 lines (130 loc) · 4.71 KB
/
music_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
import os
import traceback
import time
from threading import Lock
from PyQt5.QtCore import pyqtSlot
from debug.qthread_with_logging import QThreadWithLogging
is_run_on_posix = os.name == 'posix'
capable_gpio = is_run_on_posix
if is_run_on_posix:
import RPi.GPIO as gpio
from omxplayer.player import OMXPlayer
gpio.setwarnings(False)
else:
import subprocess
import psutil
class MusicPlayer(QThreadWithLogging):
def __init__(self):
QThreadWithLogging.__init__(self)
self.playlist = []
self.process = None
self.process_util = None
self.player = None
self.lock = Lock()
self.dbus_increment = 1
if capable_gpio:
gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)
@pyqtSlot(list)
def push_to_playlist(self, pathes):
self.log(f'lock by push_to_playlist')
with self.lock:
self.log(f'push {pathes}')
self.playlist += pathes
self.log(f'unlock')
@pyqtSlot(result=list)
def get_playlist(self):
self.log(f'lock by push_to_playlist')
with self.lock:
playlist = list(self.playlist)
self.log(f'unlock')
return playlist
@pyqtSlot()
def play_unstoppable_music(self, mp3_path):
self.log(f'begin force playing {mp3_path}')
if capable_gpio:
gpio.output(18, gpio.HIGH)
try:
if is_run_on_posix:
player = OMXPlayer(mp3_path, args='--no-keys -o local',
dbus_name=f'org.mpris.MediaPlayer2.omxplayer{self.dbus_increment}')
self.dbus_increment += 1
while player._process.returncode is None: time.sleep(1)
else:
process = subprocess.Popen(rf'python debug\run_mp3.py "{mp3_path}"')
process.wait()
process.terminate()
except:
self.log(f'failed to force playing {mp3_path}')
self.log(traceback.format_exc())
if capable_gpio:
gpio.output(18, gpio.LOW)
self.log(f'end force playing {mp3_path}')
@pyqtSlot()
def play_music(self, mp3_path):
self.log(f'begin playing {mp3_path}')
if capable_gpio:
gpio.output(18, gpio.HIGH)
try:
if is_run_on_posix:
self.player = OMXPlayer(mp3_path, args='--no-keys -o local',
dbus_name=f'org.mpris.MediaPlayer2.omxplayer{self.dbus_increment}')
self.dbus_increment += 1
while self.player._process.returncode is None: time.sleep(1)
else:
self.process = subprocess.Popen(rf'python debug\run_mp3.py "{mp3_path}"')
self.process_util = psutil.Process(pid=self.process.pid)
self.process.wait()
self.process.terminate()
except:
self.log(f'failed to playing {mp3_path}')
self.log(traceback.format_exc())
if capable_gpio:
gpio.output(18, gpio.LOW)
self.log(f'end playing {mp3_path}')
def run(self):
while not self.isFinished():
if len(self.playlist) > 0:
self.log(f'lock by running')
with self.lock:
try:
self.play_music(self.playlist[0])
except:
self.log(traceback.format_exc())
del self.playlist[0]
self.log('unlock')
@pyqtSlot()
def music_pause(self):
try:
if is_run_on_posix:
self.player.pause()
gpio.output(18, gpio.LOW)
else:
if self.process:
self.process_util.suspend()
except Exception as e:
self.log(f'failed to pause music')
self.log(traceback.format_exc())
@pyqtSlot()
def music_resume(self):
try:
if is_run_on_posix:
self.player.play()
gpio.output(18, gpio.HIGH)
else:
if self.process:
self.process_util.resume()
except Exception as e:
self.log(f'failed to pause music')
self.log(traceback.format_exc())
@pyqtSlot()
def close(self):
try:
if is_run_on_posix:
self.player.quit()
gpio.output(18, gpio.LOW)
else:
self.process.terminate()
except Exception as e:
self.log(f'failed to close music')
self.log(traceback.format_exc())