This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer_service.py
115 lines (94 loc) · 3.38 KB
/
player_service.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
from time import sleep
from oscpy.server import OSCThreadServer
from oscpy.client import OSCClient
from android.config import SERVICE_CLASS_NAME
from jnius import autoclass
from android_media_player import AndroidMediaPlayer
PythonService = autoclass(SERVICE_CLASS_NAME)
class Player:
#############
# Event Loop
#############
def run(self):
# Player and OSC state
self.server = OSCThreadServer()
self.server.listen('localhost', port=3000, default=True)
self.client = OSCClient('localhost', 3002)
self.player = AndroidMediaPlayer()
self.mService = PythonService.mService
self.playlist = []
self.now_playing = 0
self.loop_running = True
self.paused = False
# UI events
self.server.bind(b'/add_playlist', self.add_playlist)
self.server.bind(b'/terminate', self.terminate)
self.server.bind(b'/play', self.play)
self.server.bind(b'/pause', self.pause)
self.server.bind(b'/skip_next', self.skip_next)
self.server.bind(b'/skip_previous', self.skip_previous)
self.server.bind(b'/service_state', self.service_state)
# Android MediaPlayer Event
self.player.init_player(self.play_next)
# Loop
while self.loop_running:
sleep(0.1)
self.mService.stopSelf()
##################
# Event Actions
##################
def add_playlist(self, *uri_list):
for uri in uri_list:
self.playlist.append(uri)
self.service_state()
def terminate(self):
self.playlist = []
self.loop_running = False
self.service_state()
self.player.stop()
self.player.release()
self.server.terminate_server()
sleep(0.1)
self.server.close()
def play(self, *action_list):
if self.playlist:
if self.paused:
self.paused = False
self.player.resume()
else:
self.player.start(self.mService,self.playlist[self.now_playing])
def pause(self, *action_list):
if self.playlist and not self.paused:
self.player.pause()
self.paused = True
def skip_next(self):
self.player.stop()
self.play_next()
def skip_previous(self):
if self.now_playing < 1:
self.now_playing = len(self.playlist)
self.now_playing = self.now_playing -1
self.player.stop()
self.service_state()
if not self.paused:
self.player.start(self.mService, self.playlist[self.now_playing])
def play_next(self):
self.now_playing = self.now_playing +1
if self.now_playing >= len(self.playlist):
self.now_playing = 0
self.service_state()
if not self.paused:
self.player.start(self.mService, self.playlist[self.now_playing])
def service_state(self):
if self.playlist:
uri = self.playlist[self.now_playing]
else:
uri = ''.encode('utf8')
self.client.send_message(b'/track_state', [uri])
if self.playlist:
msg = 'Track ' + str(self.now_playing + 1) + ' of ' +\
str(len(self.playlist)) + ' in the Playlist.'
else:
msg = 'The Music Playlist is Empty.'
self.client.send_message(b'/playlist_state', [msg.encode('utf8')])
Player().run()