1
# Copyright (C) 2008-2009 Adam Olsen
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2, or (at your option)
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
from xl import playlist, settings, event, common
20
logger = logging.getLogger(__name__)
23
import cPickle as pickle
27
class PlayQueue(playlist.Playlist):
30
Manages the queue of songs to be played
33
def __init__(self, player, location=None):
34
self.current_playlist = None
35
self.current_pl_track = None
36
playlist.Playlist.__init__(self, name="Queue")
38
player._set_queue(self)
39
self.stop_track = None
40
if location is not None:
41
self.load_from_location(location)
43
def set_current_playlist(self, playlist):
44
self.current_playlist = playlist
46
def set_current_pl_track(self, track):
47
self.current_pl_track = track
50
track = playlist.Playlist.peek(self)
52
if self.current_playlist:
53
track = self.current_playlist.peek()
56
def next(self, player=True, track=None):
58
Goes to the next track, either in the queue, or in the current
59
playlist. If a track is passed in, that track is played
61
:param player: play the track in addition to returning it
62
:param track: if passed, play this track
65
if self.player.current and self.player.current == self.stop_track:
67
event.log_event('stop_track', self, self.stop_track)
68
self.stop_track = None
71
if not self.ordered_tracks:
72
if self.current_playlist:
73
track = self.current_playlist.next()
74
self.current_playlist.current_playing = True
75
self.current_playing = False
77
track = self.ordered_tracks.pop(0)
79
self.current_playing = True
80
if self.current_playlist:
81
self.current_playlist.current_playing = False
85
event.log_event("playback_playlist_end", self,
86
self.current_playlist)
88
self.player.play(track)
90
event.log_event("playback_playlist_end", self,
91
self.current_playlist)
96
if self.player.current:
97
if self.player.get_time() < 5:
98
if self.current_playlist:
99
track = self.current_playlist.prev()
101
track = self.player.current
103
track = self.get_current()
104
self.player.play(track)
107
def get_current(self):
108
if self.player.current and self.current_pos > 0:
109
current = self.player.current
111
current = playlist.Playlist.get_current(self)
112
if current == None and self.current_playlist:
113
current = self.current_playlist.get_current()
116
def get_current_pos(self):
119
def play(self, track=None):
121
start playback, either from the passed track or from already
124
if self.player.is_playing() and not track:
127
track = self.get_current()
129
self.player.play(track)
130
if track in self.ordered_tracks:
131
self.ordered_tracks.remove(track)
135
def _save_player_state(self, location):
137
state['state'] = self.player.get_state()
138
state['position'] = self.player.get_time()
139
state['_playtime_stamp'] = self.player._playtime_stamp
140
f = open(location, 'wb')
141
pickle.dump(state, f, protocol = 2)
145
def _restore_player_state(self, location):
146
if not settings.get_option("player/resume_playback", True):
150
f = open(location, 'rb')
151
state = pickle.load(f)
156
for req in ['state', 'position', '_playtime_stamp']:
160
if state['state'] in ['playing', 'paused']:
161
vol = self.player._get_volume()
162
self.player._set_volume(0)
165
if not self.player.current:
168
self.player.seek(state['position'])
169
if state['state'] == 'paused' or \
170
settings.get_option("player/resume_paused", False):
171
self.player.toggle_pause()
172
self.player._set_volume(vol)
173
self.player._playtime_stamp = state['_playtime_stamp']