Skip to content

Commit

Permalink
work in progress: fixing mpd.go error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tramhao committed May 20, 2021
1 parent f0a3b51 commit 6c5eb76
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 57 deletions.
2 changes: 0 additions & 2 deletions gomu.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ func (g *Gomu) quit(args Args) error {
}

if gomu.playingBar.albumPhoto != nil {
gomu.playingBar.albumPhoto.Clear()
gomu.playingBar.albumPhoto.Destroy()
gomu.playingBar.albumPhoto = nil
}

gomu.app.Stop()
Expand Down
73 changes: 25 additions & 48 deletions player/mpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewMPDPlayer(volume int, mpdPort string) (*MPDPlayer, error) {
return nil, tracerr.Wrap(err)
}

return &MPDPlayer{
client := &MPDPlayer{
hasInit: true,
isRunning: false,
vol: volume,
Expand All @@ -75,7 +75,10 @@ func NewMPDPlayer(volume int, mpdPort string) (*MPDPlayer, error) {
songSkip: func(Audio) {
},
client: mpdConn,
}, nil
}

go client.keepAlive()
return client, nil
}

// SetSongFinish accepts callback which will be executed when the song finishes.
Expand Down Expand Up @@ -121,11 +124,6 @@ func (p *MPDPlayer) Run(currSong Audio) (err error) {
p.currentSong = currSong
p.execSongStart(currSong)

if p.client == nil {
if err = p.reconnect(); err != nil {
return tracerr.Wrap(err)
}
}
status, err := p.client.Status()
if err != nil {
log.Fatalln(err)
Expand Down Expand Up @@ -165,15 +163,11 @@ func (p *MPDPlayer) Run(currSong Audio) (err error) {

go func() {
for {
if p.client == nil {
if err = p.reconnect(); err != nil {
return
}
}

status, err := p.client.Status()
if err != nil {
log.Fatalln(err)
continue
}

if status["state"] == "stop" {
Expand Down Expand Up @@ -231,11 +225,6 @@ func (p *MPDPlayer) Skip() error {
}

// drain the stream
if p.client == nil {
if err := p.reconnect(); err != nil {
return tracerr.Wrap(err)
}
}

if err := p.client.Stop(); err != nil {
return tracerr.Wrap(err)
Expand All @@ -261,11 +250,6 @@ func (p *MPDPlayer) GetPosition() time.Duration {
if !p.isRunning {
return 0
}
if p.client == nil {
if err := p.reconnect(); err != nil {
return 0
}
}

status, err := p.client.Status()
if err != nil {
Expand All @@ -284,11 +268,6 @@ func (p *MPDPlayer) GetPosition() time.Duration {

// Seek is the function to move forward and rewind
func (p *MPDPlayer) Seek(pos int) error {
if p.client == nil {
if err := p.reconnect(); err != nil {
return tracerr.Wrap(err)
}
}

if err := p.client.SeekCur(time.Duration(pos)*time.Second, false); err != nil {
return tracerr.Wrap(err)
Expand Down Expand Up @@ -331,23 +310,8 @@ func (p *MPDPlayer) VolToHuman(volume float64) int {
return int(volume*10) + 100
}

func (p *MPDPlayer) reconnect() (err error) {
p.client, err = mpd.Dial("tcp", p.mpdPort)
if err != nil {
return tracerr.Wrap(err)
}
return nil
}

// Stop is the place to stop playing
func (p *MPDPlayer) Stop() (err error) {

if p.client == nil {
if err = p.reconnect(); err != nil {
return tracerr.Wrap(err)
}
}

p.RLock()
defer p.RUnlock()
if err = p.client.Stop(); err != nil {
Expand All @@ -363,15 +327,28 @@ func (p *MPDPlayer) Stop() (err error) {
// UpdateDB update the datebase
func (p *MPDPlayer) UpdateDB() (err error) {

if p.client == nil {
if err = p.reconnect(); err != nil {
return tracerr.Wrap(err)
}
}

if _, err = p.client.Update("/"); err != nil {
return tracerr.Wrap(err)
}

return nil
}

func (p *MPDPlayer) reconnect() (err error) {
p.client, err = mpd.Dial("tcp", p.mpdPort)
if err != nil {
return tracerr.Wrap(err)
}
return nil
}

func (p *MPDPlayer) keepAlive() {
for {
err := p.client.Ping()
if err != nil {
log.Println("Disconnected. Reconnecting...")
p.reconnect()
}
time.Sleep(3 * time.Second)
}
}
8 changes: 1 addition & 7 deletions playingbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (p *PlayingBar) run() error {
continue
}

p.setProgress(int(gomu.player.GetPosition().Seconds()))
gomu.app.QueueUpdate(func() { p.setProgress(int(gomu.player.GetPosition().Seconds())) })

start, err := time.ParseDuration(strconv.Itoa(progress) + "s")
if err != nil {
Expand Down Expand Up @@ -164,9 +164,7 @@ func (p *PlayingBar) newProgress(currentSong *player.AudioFile, full int) {
p.subtitle = nil
p.mu.RUnlock()
if p.albumPhoto != nil {
p.albumPhoto.Clear()
p.albumPhoto.Destroy()
p.albumPhoto = nil
}

err := p.loadLyrics(currentSong.Path())
Expand Down Expand Up @@ -306,9 +304,7 @@ func (p *PlayingBar) loadLyrics(currentSongPath string) error {
p.tag = tag

if p.albumPhoto != nil {
p.albumPhoto.Clear()
p.albumPhoto.Destroy()
p.albumPhoto = nil
}

syltFrames := tag.GetFrames(tag.CommonID("Synchronised lyrics/text"))
Expand Down Expand Up @@ -392,9 +388,7 @@ func (p *PlayingBar) updatePhoto() {
}

if p.albumPhoto != nil {
p.albumPhoto.Clear()
p.albumPhoto.Destroy()
p.albumPhoto = nil
}

// get related size
Expand Down

0 comments on commit 6c5eb76

Please sign in to comment.