Skip to content

Commit

Permalink
fix: #151 #152
Browse files Browse the repository at this point in the history
  • Loading branch information
anhoder committed Jun 13, 2024
1 parent a1dd71e commit c6e85aa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pkg/lastfm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Report(client *Client, phase ReportPhase, song structs.Song, passedTime tim
case ReportPhaseComplete:
duration := song.Duration.Seconds()
passedSeconds := passedTime.Seconds()
if duration <= passedSeconds || passedSeconds >= duration/2 {
if passedSeconds >= duration/2 {
go func(song structs.Song, passed time.Duration) {
_ = client.Scrobble(map[string]interface{}{
"artist": song.ArtistName(),
Expand Down
25 changes: 14 additions & 11 deletions pkg/player/mpd_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewMpdPlayer(bin, configFile, network, address string) Player {
p.watch()
}()

p.SyncMpdStatus("")
p.syncMpdStatus("")
return p
}

Expand All @@ -128,7 +128,7 @@ func (p *mpdPlayer) client() *mpd.Client {
return _client
}

func (p *mpdPlayer) SyncMpdStatus(subsystem string) {
func (p *mpdPlayer) syncMpdStatus(subsystem string) {
status, err := p.client().Status()
mpdErrorHandler(err, true)

Expand All @@ -152,14 +152,17 @@ func (p *mpdPlayer) SyncMpdStatus(subsystem string) {
p.setState(Stopped)
}
}
p.volume, _ = strconv.Atoi(status["volume"])
duration, _ := time.ParseDuration(status["elapsed"] + "s")

if p.timer != nil {
p.timer.SetPassed(duration)
select {
case p.timeChan <- p.timer.Passed():
default:
if vol := status["volume"]; vol != "" {
p.volume, _ = strconv.Atoi(vol)
}
if elapsed := status["elapsed"]; elapsed != "" {
duration, _ := time.ParseDuration(elapsed + "s")
if p.timer != nil {
p.timer.SetPassed(duration)
select {
case p.timeChan <- p.timer.Passed():
default:
}
}
}
}
Expand Down Expand Up @@ -262,7 +265,7 @@ func (p *mpdPlayer) watch() {
return
case subsystem := <-p.watcher.Event:
if subsystem == "player" || subsystem == "mixer" {
p.SyncMpdStatus(subsystem)
p.syncMpdStatus(subsystem)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/state_handler/play_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ type Controller interface {
CtrlPrevious()
CtrlSeek(duration time.Duration)
CtrlSetVolume(volume int)
PlayingInfo() PlayingInfo
}
2 changes: 1 addition & 1 deletion pkg/state_handler/state_handler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
MediaTypeVedio
)

func NewHandler(p Controller) *Handler {
func NewHandler(p Controller, _ PlayingInfo) *Handler {
playingCenter := mediaplayer.MPNowPlayingInfoCenter_defaultCenter()
commandCenter := mediaplayer.MPRemoteCommandCenter_sharedCommandCenter()
commandHandler := remoteCommandHandler_new(p)
Expand Down
4 changes: 2 additions & 2 deletions pkg/state_handler/state_handler_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Handler struct {
once sync.Once
}

func NewHandler(p Controller) *Handler {
func NewHandler(p Controller, nowInfo PlayingInfo) *Handler {
handler := &Handler{
player: p,
name: fmt.Sprintf("org.mpris.MediaPlayer2.musicfox.instance%d", os.Getpid()),
Expand All @@ -61,7 +61,7 @@ func NewHandler(p Controller) *Handler {
_ = handler.dbus.Export(mp2, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2")

mprisPlayer := &Player{Handler: handler}
mprisPlayer.createStatus(p.PlayingInfo())
mprisPlayer.createStatus(nowInfo)
_ = handler.dbus.Export(mprisPlayer, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player")

_ = handler.dbus.Export(introspect.NewIntrospectable(handler.IntrospectNode()), "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Introspectable")
Expand Down
32 changes: 26 additions & 6 deletions pkg/ui/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewPlayer(model *NeteaseModel) *Player {
ctx, p.cancel = context.WithCancel(context.Background())

p.Player = player.NewPlayerFromConfig()
p.stateHandler = state_handler.NewHandler(p)
p.stateHandler = state_handler.NewHandler(p, p.PlayingInfo())

// remote control
go func() {
Expand Down Expand Up @@ -547,6 +547,14 @@ func (p *Player) PreviousSong(isManual bool) {
_ = p.PlaySong(song, DurationPrev)
}

func (p *Player) Seek(duration time.Duration) {
p.Player.Seek(duration)
if p.lrcTimer != nil {
p.lrcTimer.Rewind()
}
p.stateHandler.SetPlayingInfo(p.PlayingInfo())
}

// SetPlayMode 播放模式切换
func (p *Player) SetPlayMode(playMode player.Mode) {
if playMode > 0 {
Expand Down Expand Up @@ -738,12 +746,24 @@ func (p *Player) handleControlSignal(signal CtrlSignal) {
case CtrlNext:
p.NextSong(true)
case CtrlSeek:
p.Player.Seek(signal.Duration)
if p.lrcTimer != nil {
p.lrcTimer.Rewind()
}
p.stateHandler.SetPlayingInfo(p.PlayingInfo())
p.Seek(signal.Duration)
case CtrlRerender:
p.model.Rerender(false)
}
}

func (p *Player) PlayingInfo() state_handler.PlayingInfo {
music := p.curSong
return state_handler.PlayingInfo{
TotalDuration: music.Duration,
PassedDuration: p.PassedTime(),
State: p.State(),
Volume: p.Volume(),
TrackID: music.Id,
PicUrl: music.PicUrl,
Name: music.Name,
Album: music.Album.Name,
Artist: music.ArtistName(),
AlbumArtist: music.Album.ArtistName(),
}
}
34 changes: 11 additions & 23 deletions pkg/ui/player_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,52 @@ package ui

import (
"time"

"github.com/go-musicfox/go-musicfox/pkg/state_handler"
)

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlPaused() {
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlPaused}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlResume() {
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlResume}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlStop() {
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlStop}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlToggle() {
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlToggle}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlNext() {
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlNext}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlPrevious() {
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlPrevious}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlSeek(duration time.Duration) {
p.ctrl <- CtrlSignal{
Type: CtrlSeek,
Duration: duration,
}
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.ctrl <- CtrlSignal{Type: CtrlSeek, Duration: duration}
}

// Deprecated: Only state_handler.Handler can call this method, others please use Player instead.
func (p *Player) CtrlSetVolume(volume int) {
// 不更新playingInfo
// NOTICE: 提供给state_handler调用,因为有GC panic问题,这里使用chan传递
p.Player.SetVolume(volume)
}

func (p *Player) PlayingInfo() state_handler.PlayingInfo {
music := p.curSong
return state_handler.PlayingInfo{
TotalDuration: music.Duration,
PassedDuration: p.PassedTime(),
State: p.State(),
Volume: p.Volume(),
TrackID: music.Id,
PicUrl: music.PicUrl,
Name: music.Name,
Album: music.Album.Name,
Artist: music.ArtistName(),
AlbumArtist: music.Album.ArtistName(),
}
}

0 comments on commit c6e85aa

Please sign in to comment.