getPlayQueueLength func
This commit is contained in:
+25
-20
@@ -63,10 +63,11 @@ type playbackEngine struct {
|
|||||||
|
|
||||||
playQueue []mediaprovider.MediaItem
|
playQueue []mediaprovider.MediaItem
|
||||||
playQueueShuffle []mediaprovider.MediaItem
|
playQueueShuffle []mediaprovider.MediaItem
|
||||||
nowPlayingIdx int
|
|
||||||
isRadio bool
|
nowPlayingIdx int
|
||||||
loopMode LoopMode
|
isRadio bool
|
||||||
shuffle bool
|
loopMode LoopMode
|
||||||
|
shuffle bool
|
||||||
|
|
||||||
pauseAfterCurrent bool // flag to pause playback after current track ends
|
pauseAfterCurrent bool // flag to pause playback after current track ends
|
||||||
|
|
||||||
@@ -223,12 +224,16 @@ func (p *playbackEngine) SetPlayer(pl player.BasePlayer) error {
|
|||||||
|
|
||||||
// Interface functions for interacting with the play queue
|
// Interface functions for interacting with the play queue
|
||||||
|
|
||||||
|
func (p *playbackEngine) getPlayQueueLength() int {
|
||||||
|
return len(p.playQueue)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *playbackEngine) PlayTrackAt(idx int) error {
|
func (p *playbackEngine) PlayTrackAt(idx int) error {
|
||||||
return p.playTrackAt(idx, 0)
|
return p.playTrackAt(idx, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *playbackEngine) playTrackAt(idx int, startTime float64) error {
|
func (p *playbackEngine) playTrackAt(idx int, startTime float64) error {
|
||||||
if l := len(p.playQueue); idx < 0 || idx >= l {
|
if l := p.getPlayQueueLength(); idx < 0 || idx >= l {
|
||||||
return fmt.Errorf("track index (%d) out of range (0-%d)", idx, l)
|
return fmt.Errorf("track index (%d) out of range (0-%d)", idx, l)
|
||||||
}
|
}
|
||||||
// scrobble current track if needed
|
// scrobble current track if needed
|
||||||
@@ -241,7 +246,7 @@ func (p *playbackEngine) playTrackAt(idx int, startTime float64) error {
|
|||||||
|
|
||||||
// Gets the curently playing media item, if any.
|
// Gets the curently playing media item, if any.
|
||||||
func (p *playbackEngine) NowPlaying() mediaprovider.MediaItem {
|
func (p *playbackEngine) NowPlaying() mediaprovider.MediaItem {
|
||||||
if p.nowPlayingIdx < 0 || len(p.playQueue) == 0 || p.player.GetStatus().State == player.Stopped {
|
if p.nowPlayingIdx < 0 || p.getPlayQueueLength() == 0 || p.player.GetStatus().State == player.Stopped {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return p.playQueue[p.nowPlayingIdx]
|
return p.playQueue[p.nowPlayingIdx]
|
||||||
@@ -327,7 +332,7 @@ func (p *playbackEngine) SeekFwdBackN(n int) error {
|
|||||||
return p.player.SeekSeconds(0) // seek back in current song
|
return p.player.SeekSeconds(0) // seek back in current song
|
||||||
}
|
}
|
||||||
|
|
||||||
lastIdx := len(p.playQueue) - 1
|
lastIdx := p.getPlayQueueLength() - 1
|
||||||
newIdx := min(lastIdx, max(0, idx+n))
|
newIdx := min(lastIdx, max(0, idx+n))
|
||||||
|
|
||||||
if idx == lastIdx && n > 0 {
|
if idx == lastIdx && n > 0 {
|
||||||
@@ -392,7 +397,7 @@ func (p *playbackEngine) doLoaditems(items []mediaprovider.MediaItem, insertQueu
|
|||||||
p.nowPlayingIdx = -1
|
p.nowPlayingIdx = -1
|
||||||
p.playQueue = nil
|
p.playQueue = nil
|
||||||
}
|
}
|
||||||
if nextChanged := len(items) > 0 && (insertQueueMode != Append || (p.nowPlayingIdx == len(p.playQueue)-1)); nextChanged {
|
if nextChanged := len(items) > 0 && (insertQueueMode != Append || (p.nowPlayingIdx == p.getPlayQueueLength()-1)); nextChanged {
|
||||||
defer p.handleNextTrackUpdated()
|
defer p.handleNextTrackUpdated()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -400,7 +405,7 @@ func (p *playbackEngine) doLoaditems(items []mediaprovider.MediaItem, insertQueu
|
|||||||
rand.Shuffle(len(items), func(i, j int) { items[i], items[j] = items[j], items[i] })
|
rand.Shuffle(len(items), func(i, j int) { items[i], items[j] = items[j], items[i] })
|
||||||
}
|
}
|
||||||
|
|
||||||
insertIdx := len(p.playQueue)
|
insertIdx := p.getPlayQueueLength()
|
||||||
if insertQueueMode == InsertNext {
|
if insertQueueMode == InsertNext {
|
||||||
insertIdx = p.nowPlayingIdx + 1
|
insertIdx = p.nowPlayingIdx + 1
|
||||||
}
|
}
|
||||||
@@ -416,14 +421,14 @@ func (p *playbackEngine) LoadRadioStation(radio *mediaprovider.RadioStation, ins
|
|||||||
p.nowPlayingIdx = -1
|
p.nowPlayingIdx = -1
|
||||||
p.playQueue = nil
|
p.playQueue = nil
|
||||||
}
|
}
|
||||||
if nextChanged := insertMode == InsertNext || (insertMode == Append && p.nowPlayingIdx == len(p.playQueue)-1); nextChanged {
|
if nextChanged := insertMode == InsertNext || (insertMode == Append && p.nowPlayingIdx == p.getPlayQueueLength()-1); nextChanged {
|
||||||
p.handleNextTrackUpdated()
|
p.handleNextTrackUpdated()
|
||||||
}
|
}
|
||||||
insertIdx := len(p.playQueue)
|
insertIdx := p.getPlayQueueLength()
|
||||||
if insertMode == InsertNext {
|
if insertMode == InsertNext {
|
||||||
insertIdx = p.nowPlayingIdx + 1
|
insertIdx = p.nowPlayingIdx + 1
|
||||||
}
|
}
|
||||||
new := make([]mediaprovider.MediaItem, len(p.playQueue)+1)
|
new := make([]mediaprovider.MediaItem, p.getPlayQueueLength()+1)
|
||||||
firstHalf := p.playQueue[:insertIdx]
|
firstHalf := p.playQueue[:insertIdx]
|
||||||
copy(new, firstHalf)
|
copy(new, firstHalf)
|
||||||
new[len(firstHalf)] = radio
|
new[len(firstHalf)] = radio
|
||||||
@@ -435,7 +440,7 @@ func (p *playbackEngine) LoadRadioStation(radio *mediaprovider.RadioStation, ins
|
|||||||
|
|
||||||
// Stop playback and clear the play queue.
|
// Stop playback and clear the play queue.
|
||||||
func (p *playbackEngine) StopAndClearPlayQueue() {
|
func (p *playbackEngine) StopAndClearPlayQueue() {
|
||||||
changed := len(p.playQueue) > 0
|
changed := p.getPlayQueueLength() > 0
|
||||||
p.player.Stop(false)
|
p.player.Stop(false)
|
||||||
p.playQueue = nil
|
p.playQueue = nil
|
||||||
p.nowPlayingIdx = -1
|
p.nowPlayingIdx = -1
|
||||||
@@ -498,7 +503,7 @@ func (p *playbackEngine) UpdatePlayQueue(items []mediaprovider.MediaItem) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *playbackEngine) RemoveTracksFromQueue(idxs []int) {
|
func (p *playbackEngine) RemoveTracksFromQueue(idxs []int) {
|
||||||
newQueue := make([]mediaprovider.MediaItem, 0, len(p.playQueue)-len(idxs))
|
newQueue := make([]mediaprovider.MediaItem, 0, p.getPlayQueueLength()-len(idxs))
|
||||||
idxSet := sharedutil.ToSet(idxs)
|
idxSet := sharedutil.ToSet(idxs)
|
||||||
isPlayingTrackRemoved := false
|
isPlayingTrackRemoved := false
|
||||||
isNextPlayingTrackremoved := false
|
isNextPlayingTrackremoved := false
|
||||||
@@ -593,7 +598,7 @@ func (p *playbackEngine) cacheNextTracks() {
|
|||||||
// the "currently" playing track, since we're probably about to play it
|
// the "currently" playing track, since we're probably about to play it
|
||||||
npI := max(p.nowPlayingIdx, 0)
|
npI := max(p.nowPlayingIdx, 0)
|
||||||
for _, idx := range [3]int{npI, npI + 1, npI + 2} {
|
for _, idx := range [3]int{npI, npI + 1, npI + 2} {
|
||||||
if idx > 0 && idx < len(p.playQueue) {
|
if idx > 0 && idx < p.getPlayQueueLength() {
|
||||||
item := p.playQueue[idx]
|
item := p.playQueue[idx]
|
||||||
if item.Metadata().Type == mediaprovider.MediaItemTypeTrack {
|
if item.Metadata().Type == mediaprovider.MediaItemTypeTrack {
|
||||||
fetch = append(fetch, AudioCacheRequest{
|
fetch = append(fetch, AudioCacheRequest{
|
||||||
@@ -622,7 +627,7 @@ func (p *playbackEngine) handleOnTrackChange() {
|
|||||||
}
|
}
|
||||||
if p.pendingTrackChangeNum < 0 && (p.wasStopped || p.loopMode != LoopOne) {
|
if p.pendingTrackChangeNum < 0 && (p.wasStopped || p.loopMode != LoopOne) {
|
||||||
p.nowPlayingIdx++
|
p.nowPlayingIdx++
|
||||||
if p.loopMode == LoopAll && p.nowPlayingIdx == len(p.playQueue) {
|
if p.loopMode == LoopAll && p.nowPlayingIdx == p.getPlayQueueLength() {
|
||||||
p.nowPlayingIdx = 0 // wrapped around
|
p.nowPlayingIdx = 0 // wrapped around
|
||||||
}
|
}
|
||||||
} else if p.pendingTrackChangeNum >= 0 {
|
} else if p.pendingTrackChangeNum >= 0 {
|
||||||
@@ -681,14 +686,14 @@ func (p *playbackEngine) handleNextTrackUpdated() {
|
|||||||
func (p *playbackEngine) nextPlayingIndex() int {
|
func (p *playbackEngine) nextPlayingIndex() int {
|
||||||
switch p.loopMode {
|
switch p.loopMode {
|
||||||
case LoopNone:
|
case LoopNone:
|
||||||
if p.nowPlayingIdx >= len(p.playQueue)-1 {
|
if p.nowPlayingIdx >= p.getPlayQueueLength()-1 {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
return p.nowPlayingIdx + 1
|
return p.nowPlayingIdx + 1
|
||||||
case LoopOne:
|
case LoopOne:
|
||||||
return p.nowPlayingIdx
|
return p.nowPlayingIdx
|
||||||
case LoopAll:
|
case LoopAll:
|
||||||
if p.nowPlayingIdx >= len(p.playQueue)-1 {
|
if p.nowPlayingIdx >= p.getPlayQueueLength()-1 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return p.nowPlayingIdx + 1
|
return p.nowPlayingIdx + 1
|
||||||
@@ -765,7 +770,7 @@ func (p *playbackEngine) setNextTrack(idx int) error {
|
|||||||
|
|
||||||
// call BEFORE updating p.nowPlayingIdx
|
// call BEFORE updating p.nowPlayingIdx
|
||||||
func (p *playbackEngine) checkScrobble() {
|
func (p *playbackEngine) checkScrobble() {
|
||||||
if !p.scrobbleCfg.Enabled || len(p.playQueue) == 0 || p.nowPlayingIdx < 0 {
|
if !p.scrobbleCfg.Enabled || p.getPlayQueueLength() == 0 || p.nowPlayingIdx < 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
track, ok := p.playQueue[p.nowPlayingIdx].(*mediaprovider.Track)
|
track, ok := p.playQueue[p.nowPlayingIdx].(*mediaprovider.Track)
|
||||||
@@ -794,7 +799,7 @@ func (p *playbackEngine) checkScrobble() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *playbackEngine) sendNowPlayingScrobble() {
|
func (p *playbackEngine) sendNowPlayingScrobble() {
|
||||||
if !p.scrobbleCfg.Enabled || len(p.playQueue) == 0 || p.nowPlayingIdx < 0 {
|
if !p.scrobbleCfg.Enabled || p.getPlayQueueLength() == 0 || p.nowPlayingIdx < 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
track, ok := p.playQueue[p.nowPlayingIdx].(*mediaprovider.Track)
|
track, ok := p.playQueue[p.nowPlayingIdx].(*mediaprovider.Track)
|
||||||
|
|||||||
Reference in New Issue
Block a user