From 1a04fd8f3429e8aa0e245e58b849ba82a24d4f83 Mon Sep 17 00:00:00 2001 From: Himura Kazuto Date: Sun, 23 Jul 2017 19:37:32 +0300 Subject: [PATCH] #7 seems to be OK. Menu items and will be beta --- background_music_player.py | 51 ++++++++++++++++++++++++++------------ main.py | 8 +++--- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/background_music_player.py b/background_music_player.py index 8695779..e0023e1 100644 --- a/background_music_player.py +++ b/background_music_player.py @@ -5,6 +5,8 @@ import wx import wx.grid +from constants import Colors + class BackgroundMusicPlayer(object): def __init__(self, parent): @@ -18,7 +20,7 @@ def __init__(self, parent): self.player.audio_set_mute(False) self.window = BackgroundMusicFrame(self.parent) # None self.playlist = None - self._current_track_i = -1 + self.current_track_i = -1 self.fade_in_out = True def window_exists(self): @@ -50,27 +52,43 @@ def load_playlist_to_grid(self): self.window.grid.AutoSize() self.window.Layout() self.window.play_btn.Enable(True) + if self.parent.bg_player.player.get_state() in range(5): # If playing + self.window.grid.SetCellBackgroundColour(self.current_track_i, 0, Colors.DUP_ROW) - def select_track(self): + def select_track(self, select_next=False): if not self.playlist: return if self.window_exists(): - self._current_track_i = self.window.grid.GetSelectedRows()[0] - else: - self._current_track_i = (self._current_track_i + 1) % len(self.playlist) - return self._current_track_i + self.current_track_i = self.window.grid.GetSelectedRows()[0] + if not self.window_exists() or select_next: + self.current_track_i = (self.current_track_i + 1) % len(self.playlist) + return self.current_track_i + + # TODO: Async!!! + def _fade(self, vol_range, delay): + for i in vol_range: + self.player.audio_set_volume(i) + vol_msg = 'Vol: %d' % self.player.audio_get_volume() + self.parent.bg_player_status('Fading in... ' + vol_msg) + time.sleep(delay) + + def fade_in(self, delay): + self._fade(range(0, self.volume + 1, 1), delay) + + def fade_out(self, delay): + self._fade(range(self.volume, 0, -1), delay) def play(self): if not self.playlist: return - self.player.set_media(self.vlc_instance.media_new(self.playlist[self.select_track()]['path'])) + self.player.set_media(self.vlc_instance.media_new(self.playlist[self.current_track_i]['path'])) if self.player.play() != 0: # [Play] button is pushed here! return state = self.player.get_state() start = time.time() - while state != vlc.State.Playing: + while state != vlc.State.Playing: # TODO: Async!!! state = self.player.get_state() status = "%s [%fs]" % (self.parent.player_state_parse(state), (time.time() - start)) self.parent.bg_player_status(status) @@ -80,10 +98,11 @@ def play(self): if self.window_exists(): self.window.pause_btn.Enable(True) + self.window.grid.SetCellBackgroundColour(self.current_track_i, 0, Colors.DUP_ROW) volume = 0 if self.fade_in_out else self.volume start = time.time() - while self.player.audio_get_volume() != volume: + while self.player.audio_get_volume() != volume: # TODO: Async!!! self.player.audio_set_mute(False) self.player.audio_set_volume(volume) status = "Trying to unmute... [%fs]" % (time.time() - start) @@ -91,11 +110,7 @@ def play(self): time.sleep(0.002) if self.fade_in_out: - for i in range(0, self.volume + 1, 1): - self.player.audio_set_volume(i) - vol_msg = 'Vol: %d' % self.player.audio_get_volume() - self.parent.bg_player_status('Fading in... ' + vol_msg) - time.sleep(0.05) + self.fade_in(0.05) self.parent.bg_player_status("%s Vol:%d" % (self.parent.player_state_parse(self.player.get_state()), @@ -105,11 +120,14 @@ def pause(self, paused): if not self.playlist: return - if self.fade_in_out: - pass + if self.fade_in_out and paused: + self.fade_out(0.01) self.player.set_pause(paused) + if self.fade_in_out and not paused: + self.fade_in(0.01) + if not paused: self.parent.timer_start(self.timer_update_ms) @@ -168,6 +186,7 @@ def __init__(self, parent): self.grid.SetRowLabelSize(20) self.grid.SetSelectionMode(wx.grid.Grid.wxGridSelectRows) + def select_row(e): row = e.Row if hasattr(e, 'Row') else e.TopRow self.grid.Unbind(wx.grid.EVT_GRID_RANGE_SELECT) diff --git a/main.py b/main.py index 50c66c4..a77161c 100644 --- a/main.py +++ b/main.py @@ -631,6 +631,7 @@ def fade_switched(self, e): self.bg_player.window.fade_in_out_switch.SetValue(value) def background_play(self, e=None): + self.bg_player.select_track() self.bg_player.play() def background_pause(self, e=None, paused=None): @@ -655,14 +656,11 @@ def timer_start(self, val): self.bg_player_timer.Stop() def on_background_timer(self, e): - player = self.bg_player.player length, time = player.get_length(), player.get_time() time_remaining = '-%02d:%02d' % divmod(length / 1000 - time / 1000, 60) - print 'bg_timer', time - if self.bg_player.window_exists(): self.bg_player.window.time_slider.SetRange(0, length) self.bg_player.window.time_slider.SetValue(time) @@ -682,7 +680,9 @@ def on_background_timer(self, e): if self.bg_player.window_exists(): self.bg_player.window.time_slider.SetValue(0) - # Switch to next + self.bg_player.window.grid.SetCellBackgroundColour(self.bg_player.current_track_i, 0,Colors.FILTERED_GRID) + self.bg_player.select_track(True) + self.background_play() if __name__ == "__main__": app = wx.App(False)