-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoplayer.py
275 lines (233 loc) · 10.1 KB
/
autoplayer.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
#!/usr/bin/python3
import sys
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
import os
import json
import time
import vlc
import autoplayerfunc
from webapiplayer import *
# Swap to working folder
os.chdir('/home/pi/AudioPlayer')
_fileConfig = autoplayerfunc.FileConfig
"""Config File filename link"""
_fileStatus = autoplayerfunc.FileStatus
"""Status File filename link"""
# VLC player Init
args = []
aPlayer = vlc.MediaPlayer()
aInstance = vlc.Instance()
aPlayer = aInstance.media_player_new()
aPlayerList = aInstance.media_list_player_new()
aPlayerPlayTime = 0
currentsourceid = 0
currentotherurl = ''
#currentothertitle = ''
#currentotherimage = ''
def fnPlayerCallback(action):
print("fnPlayerCallback: " + action)
global aPlayer
global aPlayerList
if (action == "RELOADCONFIG"):
print('{} Reload Config Triggered'.format(time.strftime('%H:%M')))
autoplayerfunc.fnLoadConfig()
elif (action == "STOP"):
fnPlayerActionStop()
elif (action == "NEXT"):
if aPlayerList:
if aPlayerList.is_playing():
aPlayerList.next()
elif (action == "PLAY"):
if aPlayer:
if aPlayer.get_media() and aPlayer.is_playing():
aPlayer.play()
if aPlayerList:
if aPlayerList.is_playing():
aPlayerList.play()
else:
fnPlayerActionStop()
currentsourceid = int(action)
def fnPlayerActionStop():
""" Player Control - Action Stop """
global aPlayer
global aPlayerList
if aPlayer:
if aPlayer.get_media() and aPlayer.is_playing():
aPlayer.stop()
if aPlayerList:
if aPlayerList.is_playing():
aPlayerList.stop()
def fnPlayerActionPlaySource(newsourceid):
""" Player Control - Action play source id"""
global currentsourceid
global aPlayer
global aPlayerList
if currentsourceid != newsourceid:
# Stop Existing Player
fnPlayerActionStop()
currentsourceid = newsourceid
newsource = autoplayerfunc.fnGetSource(newsourceid)
if newsource is not None:
print('{} New Source Station: {}'.format(time.strftime('%H:%M'), newsource['name']))
if str(newsource['url']).startswith("upnp") or str(newsource['url']).endswith("m3u") or str(newsource['url']).endswith("m3u8"):
aMedialist = aInstance.media_list_new([str(newsource['url'])])
aPlayerList.set_media_list(aMedialist)
aPlayerList.play()
else:
aMedia = aInstance.media_new(str(newsource['url']))
aPlayer.set_media(aMedia)
aPlayer.play()
def fnPlayerActionPlayOther(newsourceurl):
""" Player Control - Action play other source"""
global currentotherurl
global aPlayer
global aPlayerList
if currentotherurl != newsourceurl:
# Stop Existing Player
fnPlayerActionStop()
currentotherurl = newsourceurl
if currentotherurl != '':
print('{} New Other Station: {}'.format(time.strftime('%H:%M'), currentotherurl))
if str(currentotherurl).startswith("upnp") or str(currentotherurl).endswith("m3u") or str(currentotherurl).endswith("m3u8"):
aMedialist = aInstance.media_list_new([str(currentotherurl)])
aPlayerList.set_media_list(aMedialist)
aPlayerList.play()
else:
aMedia = aInstance.media_new(str(currentotherurl))
aPlayer.set_media(aMedia)
aPlayer.play()
# ###########################
# Sub Main()
autoplayerfunc.fnLoadConfig()
#Init and Start Control Web Server
oWebApp = WebServer(autoplayerfunc.Config_json['webapi']['port'])
time.sleep(1)
oWebApp.ActionCallback(fnPlayerCallback)
oWebApp.start()
time.sleep(1)
# Play Control loop
while currentsourceid is not None:
#Quick info
status_json = {}
status_json['dt'] = int(time.time())
status_json['mode'] = autoplayerfunc.Config_json['manual']['mode']
status_json['currentsourceid'] = currentsourceid
status_json['currentotherurl'] = currentotherurl
status_json['source'] = autoplayerfunc.fnGetSource(currentsourceid)
if aPlayer:
status_json['aPlayer'] = str(aPlayer.get_state())
status_json['playtime'] = autoplayerfunc.fnConvertPlaytime(aPlayer.get_time())
status_json['playlength'] = autoplayerfunc.fnConvertPlaytime(aPlayer.get_length())
aPlayerMedia = aPlayer.get_media()
if aPlayerMedia:
status_json['title'] = "{} - {}".format(aPlayerMedia.get_meta(0), aPlayerMedia.get_meta(1))
status_json['subtitle'] = autoplayerfunc.fnGetSourceProgrammeTitle(currentsourceid)
status_json['mrl'] = aPlayerMedia.get_mrl()
status_json['image'] = aPlayerMedia.get_meta(15)
#for i in range(36):
# print("aPlayer Media {} : {}".format(i, aPlayerMedia.get_meta(i)))
if aPlayerList:
status_json['aPlayerList'] = str(aPlayerList.get_state())
if aPlayerList.is_playing():
aPlayerListPlayer = aPlayerList.get_media_player()
if aPlayerListPlayer:
status_json['playtime'] = autoplayerfunc.fnConvertPlaytime(aPlayerListPlayer.get_time())
status_json['playlength'] = autoplayerfunc.fnConvertPlaytime(aPlayerListPlayer.get_length())
aPlayerListMedia = aPlayerListPlayer.get_media()
if aPlayerListMedia:
status_json['title'] = "{} - {}".format(aPlayerListMedia.get_meta(0), aPlayerListMedia.get_meta(1))
status_json['subtitle'] = autoplayerfunc.fnGetSourceProgrammeTitle(currentsourceid)
status_json['mrl'] = aPlayerListMedia.get_mrl()
status_json['image'] = aPlayerListMedia.get_meta(15)
#for i in range(36):
# print("aPlayerListMedia Media {} : {}".format(i, aPlayerListMedia.get_meta(i)))
try:
with open(_fileStatus, 'w') as fp:
json.dump(status_json, fp)
except Exception as ex:
print("subMain(Status Save) ", ex)
# Check Action
if status_json['mode'] == 1:
# Full stop; play nothing, schedule paused
fnPlayerActionStop()
currentsourceid = 0
currentotherurl = ''
elif status_json['mode'] == 2:
# Stop; play nothing, resume at next schedule change
fnPlayerActionStop()
currentsourceid = 0
currentotherurl = ''
if autoplayerfunc.Config_json['manual']['schedule'] != autoplayerfunc.fnGetCurrentSchedule():
autoplayerfunc.fnSaveConfigSetMode(0)
elif status_json['mode'] == 3:
# Stop; play nothing, resume at next day
fnPlayerActionStop()
currentsourceid = 0
currentotherurl = ''
if autoplayerfunc.Config_json['manual']['day'] != autoplayerfunc.fnGetCurrentWeekday():
autoplayerfunc.fnSaveConfigSetMode(0)
elif status_json['mode'] == 11:
# Play Source; schedule paused
currentotherurl = ''
manualsourceid = autoplayerfunc.Config_json['manual']['source']
fnPlayerActionPlaySource(manualsourceid)
elif status_json['mode'] == 12:
# Play Source; resume at next schedule change
currentotherurl = ''
manualsourceid = autoplayerfunc.Config_json['manual']['source']
fnPlayerActionPlaySource(manualsourceid)
if autoplayerfunc.Config_json['manual']['schedule'] != autoplayerfunc.fnGetCurrentSchedule():
autoplayerfunc.fnSaveConfigSetMode(0)
elif status_json['mode'] == 13:
# Play Source; resume at next day
currentotherurl = ''
manualsourceid = autoplayerfunc.Config_json['manual']['source']
fnPlayerActionPlaySource(manualsourceid)
if autoplayerfunc.Config_json['manual']['day'] != autoplayerfunc.fnGetCurrentWeekday():
autoplayerfunc.fnSaveConfigSetMode(0)
elif status_json['mode'] == 21:
# Play Url; schedule paused
currentsourceid = 0
manualsourceurl = autoplayerfunc.Config_json['manual']['url']
fnPlayerActionPlayOther(manualsourceurl)
elif status_json['mode'] == 22:
# Play Url; resume at next schedule change
currentsourceid = 0
manualsourceurl = autoplayerfunc.Config_json['manual']['url']
fnPlayerActionPlayOther(manualsourceurl)
if autoplayerfunc.Config_json['manual']['schedule'] != autoplayerfunc.fnGetCurrentSchedule():
autoplayerfunc.fnSaveConfigSetMode(0)
elif status_json['mode'] == 23:
# Play Url; resume at next day
currentsourceid = 0
manualsourceurl = autoplayerfunc.Config_json['manual']['url']
fnPlayerActionPlayOther(manualsourceurl)
if autoplayerfunc.Config_json['manual']['day'] != autoplayerfunc.fnGetCurrentWeekday():
autoplayerfunc.fnSaveConfigSetMode(0)
else:
# (Default) Normal running as per schedule
currentotherurl = ''
newsourceid = autoplayerfunc.fnGetCurrentSchedule()
fnPlayerActionPlaySource(newsourceid)
#Double Check is playing (if needed) (after waiting for it to start...)
time.sleep(10)
if currentsourceid != 0:
if (not aPlayer.is_playing() and not aPlayerList.is_playing()):
print('{} Not Playing'.format(time.strftime('%H:%M')))
currentsourceid = 0
else:
if (aPlayerPlayTime > 0 and aPlayerPlayTime == status_json['playtime']):
print('{} Not Playing, time sensor'.format(time.strftime('%H:%M')))
currentsourceid = 0
aPlayerPlayTime = 0
else:
aPlayerPlayTime = status_json['playlength']
else:
fnPlayerActionStop()
#Let Play and snooze
time.sleep(10)
# See if need to reload config
if (int(time.time()) - int(autoplayerfunc.Config_json['dt'])) > autoplayerfunc.Config_json['reloadtimeout']:
print('{} Reload Config'.format(time.strftime('%H:%M')))
autoplayerfunc.fnLoadConfig()