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 fc9999f commit 5e53d75
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
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
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 5e53d75

Please sign in to comment.