-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmplayer.py
102 lines (88 loc) · 3.25 KB
/
mplayer.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
import subprocess
import thread
import time
import signal
import os
import re
from state import *
class MplayerProcess():
def __init__(self, radio, title_callback):
self.radio = radio
self.pipe = None
self.title_callback = title_callback
self.read_thread = None
def start(self):
arguments = ['mplayer', '-quiet', '-slave','-cache','128']
uri = self.radio.stream_url
uril = uri.lower()
if (uril.endswith('.m3u') or uril.endswith('.pls')):
arguments.append( '-playlist')
arguments.append(uri)
print 'Starting ' + ' '.join(arguments)
self.pipe = subprocess.Popen(arguments, stdin=subprocess.PIPE, stdout=subprocess.PIPE )
thread.start_new_thread(self.reader, ())
def stop(self):
try:
if(self.pipe != None):
self.pipe.stdin.write('Q\n')
if self.pipe.poll() == None:
time.sleep(1)
self.pipe.terminate()
self.pipe = None
except:
logging.error("Error in stop", exc_info=True)
def reader(self):
try:
while (True):
line = self.pipe.stdout.readline()
if line:
if line.startswith('ICY Info:'):
info = line.split(':', 1)[1].strip()
attrs = dict(re.findall("(\w+)='([^']*)'", info))
title = attrs.get('StreamTitle', '(none)')
print 'Stream title: ' + title
try:
v = title.decode('utf-8')
except UnicodeDecodeError:
title = title.decode('latin-1').encode('utf-8')
self.title_callback(title)
else:
break
except:
logging.error("Error in stop", exc_info=True)
class Mplayer():
#the current running mplayer process - none if no mplayer process is running
currentProcess = None
lock = threading.RLock()
#starts mplayer with a given media URI
#stops the running mplayer process
@staticmethod
def start(radio):
Mplayer.lock.acquire()
try:
Mplayer.stop()
Mplayer.currentProcess = MplayerProcess(radio, state.player_state.add_title);
state.player_state.set_radio(radio)
Mplayer.currentProcess.start()
finally:
Mplayer.lock.release()
#stops mplayer
@staticmethod
def stop():
Mplayer.lock.acquire()
try:
try:
if(Mplayer.currentProcess != None):
Mplayer.currentProcess.stop();
except:
logging.error("Error in stop", exc_info=True)
finally:
state.player_state.set_radio(None)
Mplayer.currentProcess = None
finally:
Mplayer.lock.release()
if __name__ == "__main__":
radio = Radio("3", "Swiss Jazz", "http://www.radioswissjazz.ch/live/mp3.m3u")
Mplayer.start(radio)
c = raw_input("Eingabe.")
Mplayer.stop()