annotate vixm/player.py @ 37:87aa31e41f4a master

Play the files in a separate thread
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sat, 04 Nov 2006 15:31:32 -0500
parents c6a3a4d09a9a
children 06b5a7db3d19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
1 import time
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
2 from threading import Thread
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
3
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
4 import mad, ao
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
5
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
6 class playerThread(Thread):
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
7 def __init__(self):
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
8 Thread.__init__(self)
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
9
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
10 self.shutdown = False
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 30
diff changeset
11 self.playing = False
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 30
diff changeset
12
34
8028f2addb00 Destroy all traces of xmms
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
13 self.current = None
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
14
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
15 self.blksize = 40960
36
c6a3a4d09a9a Small fix & change output to OSS
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 35
diff changeset
16 self.dev = ao.AudioDevice("oss")
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
17
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 30
diff changeset
18 def play(self, s):
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
19 self.playing = True
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
20 self.current = s
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
21
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
22 def run(self):
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
23 while not self.shutdown:
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
24 if self.current and self.playing:
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
25 # play the file
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
26 mf = mad.MadFile(self.current["file"])
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
27
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
28 while True:
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
29 buf = mf.read(self.blksize)
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
30 if buf is None or self.shutdown:
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
31 break
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
32 self.dev.play(buf, len(buf))
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
33 self.playing = False
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
34 else:
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
35 # nothing to play, just sleep
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
36 time.sleep(0.5)
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
37