-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaPlayer.py
97 lines (86 loc) · 3.01 KB
/
MediaPlayer.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
"""The stockTunes program
This is a script that will act as the MediaPlayer
It is the heart of the program and will handle the event management
"""
__author__ = 'Eli Katz'
__version__= '1.0'
import time, random
import pyglet
import Player
import StockManager
class MediaPlayer():
"""
Media Player Class
It uses Event Managers to essentially recursively call changes on itself
It handles errors by forwarding to the next song
"""
def __init__(self, flag):
"""Constructor takes a test flag and then creates the Players and Stock Manager
"""
self.happyplayer = Player.Player('happy')
self.sadplayer = Player.Player('sad')
self.clockTime = 30
self.test = flag
self.stocks = StockManager.StockManager()
def playHappy(self):
"""Plays the Happy Playlist
It calls the scheduler of pyglet to make sure
that the check is called during the app.run
"""
self.sadplayer.pause()
self.happyplayer.play()
pyglet.clock.schedule_once(self.check, self.clockTime)
pyglet.app.run()
def playSad(self):
"""Plays the Sad Playlist
It calls the scheduler of pyglet to make sure
that the check is called during the app.run
"""
self.happyplayer.pause()
self.sadplayer.play()
pyglet.clock.schedule_once(self.check, self.clockTime)
pyglet.app.run()
def check(self, dt):
"""Checks for changes in the condition. A positive change will trigger
the happy playlist while a negative change will trigger a sad playlist
In the event of an error it will attempt to play the next song
"""
if (self.playCond()):
try:
print "Positive Change"
pyglet.app.exit()
pyglet.clock.unschedule(self.check)
self.playHappy()
except:
print "Happy Song Error - Next Song"
self.happyplayer.loadSongs()
self.happyplayer.next()
pyglet.app.exit()
pyglet.clock.unschedule(self.check)
self.playHappy()
else:
try:
print "Negative Change"
pyglet.app.exit()
pyglet.clock.unschedule(self.check)
self.playSad()
except:
print "Sad Song Error - Next Song"
self.sadplayer.loadSongs()
self.sadplayer.next()
pyglet.app.exit()
pyglet.clock.unschedule(self.check)
self.playSad()
def playCond(self):
"""
Test the condition of the change whether the stock change or through a random
number
"""
if (self.test):
return (random.randint(-5, 5) >= 0)
else:
return (self.stocks.getChange >= 0)
def start(self):
"""Start the media player
"""
self.check(1)