-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathplayer_core.py
73 lines (59 loc) · 1.63 KB
/
player_core.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
# -*- coding:utf8 -*-
import time
import threading
import subprocess
class player(object):
def __init__(self):
print 'init'
self.playlist = []
self.handle = None
self._start()
pass
def __del__(self):
del self.t
pass
def _player_thread(self):
while True:
if self.playlist:
self._play(self.playlist.pop(0)['source'])
pass
def _start(self):
self.t = threading.Thread(target=self._player_thread, args=())
self.t.start()
pass
def _play(self, source):
self.handle = subprocess.Popen(['mplayer',source], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
while True:
time.sleep(1)
for l in self.handle.stdout:
if l.startswith('Exit'):
return
break
pass
pass
pass
def play(self, source):
self.playlist.insert(0, {'source':source})
self.next()
pass
def add(self, source, title):
self.playlist.append({'source':source, 'title':title})
pass
def next(self):
self.handle.stdin.write('q')
def remove(self, list_id):
if list_id < len(self.playlist):
self.playlist.pop(list_id)
pass
pass
def get_list(self):
return self.playlist
pass
if __name__ == '__main__':
p = player()
time.sleep(5)
p.add('http://m1.music.126.net/76Tcz9oo83Diol_rTKPJ8g==/1187472558006034.mp3')
p.add('/Users/jialeicui/Music/Drive By.mp3')
time.sleep(5)
p.next()
time.sleep(5)