From ecf5ed628cb9f9d6bb79ca7a6774dd6bcff07d09 Mon Sep 17 00:00:00 2001 From: raziman Date: Thu, 25 Jun 2020 14:12:02 +0800 Subject: [PATCH] refactor --- player.go | 53 ++++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/player.go b/player.go index 5b280c7..132be56 100644 --- a/player.go +++ b/player.go @@ -171,41 +171,16 @@ func (p *Player) Run() { p.playingBar.NewProgress(song.name, int(p.length.Seconds()), 100) p.playingBar.Run() - go func() { - - i := 0 - - next: - for { - - select { - case <-p.isSkipped: - break next - - case <-time.After(time.Second): - // stop progress bar from progressing when paused - if !p.IsRunning { - continue - } - - i++ - p.playingBar.progress <- 1 - - - if i > p.playingBar.full { - break next - } - - } - - } - - }() + // is used to send progress + i := 0 next: - for { + select { + case <-p.isSkipped: + break next + case <-done: close(done) p.position = 0 @@ -218,13 +193,29 @@ next: go p.Run() } break next + case <-time.After(time.Second): + // stop progress bar from progressing when paused + if !p.IsRunning { + continue + } + + i++ + p.playingBar.progress <- 1 + speaker.Lock() p.position = position() speaker.Unlock() + + if i > p.playingBar.full { + break next + } + } + } + } func (p *Player) Pause() {