From 98ff43054fe94f333e2deda2906cd62593ded1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 27 Jun 2016 10:27:35 +0200 Subject: Put time duration in playlist --- helpers/__init__.py | 7 +++++++ helpers/action.py | 9 ++++++--- helpers/music_file.py | 13 +++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) (limited to 'helpers') diff --git a/helpers/__init__.py b/helpers/__init__.py index 70667fd..0f819e7 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -9,3 +9,10 @@ def path(): path = os.path.dirname(os.path.realpath(__file__)) return path + "/../" +def duration_to_min_sec(duration): + minutes = int(duration / 60) + seconds = int(duration) % 60 + if minutes < 100: + return "{:2}:{:0>2}".format(minutes, seconds) + else: + return "{}:{:0>2}".format(minutes, seconds) diff --git a/helpers/action.py b/helpers/action.py index aff61e7..327d42f 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -31,7 +31,6 @@ class Action: def run(self): print(self.description()) getattr(self, self.action)(**self.arguments) - #pygame.event.post(pygame.event.Event(pygame.USEREVENT)) def description(self): return getattr(self, self.action + "_print")(**self.arguments) @@ -43,13 +42,17 @@ class Action: if music is not None: music.pause() else: - pygame.mixer.pause() + for music in self.key.parent.open_files.values(): + if music.is_playing() and not music.is_paused(): + music.pause() def unpause(self, music = None, **kwargs): if music is not None: music.unpause() else: - pygame.mixer.unpause() + for music in self.key.parent.open_files.values(): + if music.is_playing() and music.is_paused(): + music.unpause() def play(self, music = None, fade_in = 0, start_at = 0, restart_if_running = False, volume = 100, **kwargs): diff --git a/helpers/music_file.py b/helpers/music_file.py index 892519d..9acdedf 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -2,6 +2,7 @@ import threading import pydub import pygame import math +import time class MusicFile: def __init__(self, filename, lock, channel_id, name = None, gain = 1): @@ -32,6 +33,15 @@ class MusicFile: def is_paused(self): return self.flag_paused + @property + def sound_position(self): + if self.is_playing() and not self.is_paused(): + return min(time.time() - self.started_at, self.sound_duration) + elif self.is_playing() and self.is_paused(): + return min(self.paused_at - self.started_at, self.sound_duration) + else: + return 0 + def play(self, fade_in = 0, volume = 100, start_at = 0): self.channel().set_endevent() self.channel().set_endevent(pygame.USEREVENT) @@ -45,14 +55,17 @@ class MusicFile: else: sound = pygame.mixer.Sound(self.raw_data) + self.started_at = time.time() self.channel().play(sound, fade_ms = int(fade_in * 1000)) self.flag_paused = False def pause(self): + self.paused_at = time.time() self.channel().pause() self.flag_paused = True def unpause(self): + self.started_at += (time.time() - self.paused_at) self.channel().unpause() self.flag_paused = False -- cgit v1.2.3