-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Play Next or Previous from Queue #153
Comments
Hello! I also had such question. extension SAPlayer {
private static var _playedQueue = [SAAudioQueueItem]()
var playedQueue: [SAAudioQueueItem] {
get {
return SAPlayer._playedQueue
}
set(newValue) {
SAPlayer._playedQueue = newValue
}
}
func next() {
// Check if we have something in queue
guard audioQueued.count > 0 else { return }
// Getting next audio
let nextAudioURL = audioQueued.removeFirst()
// Clear player state
clear()
// Setting new media info for lockscreen
mediaInfo = nextAudioURL.mediaInfo
// add played audio to played queue to have eability to play prev audio
playedQueue.append(nextAudioURL)
// Init player with new source
switch nextAudioURL.loc {
case .remote:
startRemoteAudio(withRemoteUrl: nextAudioURL.url, bitrate: nextAudioURL.bitrate, mediaInfo: nextAudioURL.mediaInfo)
break
case .saved:
startSavedAudio(withSavedUrl: nextAudioURL.url, mediaInfo: nextAudioURL.mediaInfo)
break
}
// Start playback
play()
}
func prev() {
// Check if we have something in queue
guard playedQueue.count > 1 else { return }
// Getting previous audio
let nextAudioURL = playedQueue.removeLast()
// Clear player state
clear()
// append to next queue
audioQueued.insert(nextAudioURL, at: 0)
//setup new source
let currentAudioURL = playedQueue.last! // Have to be at least one element
// Setting new media info for lockscreen
mediaInfo = currentAudioURL.mediaInfo
// Init player with new source
switch currentAudioURL.loc {
case .remote:
startRemoteAudio(withRemoteUrl: currentAudioURL.url, bitrate: currentAudioURL.bitrate, mediaInfo: currentAudioURL.mediaInfo)
break
case .saved:
startSavedAudio(withSavedUrl: currentAudioURL.url, mediaInfo: currentAudioURL.mediaInfo)
break
}
// Start playback
play()
}
func addSavedToPlayedQueue(withSavedUrl url: URL, mediaInfo: SALockScreenInfo?) {
playedQueue.append(SAAudioQueueItem(loc: .saved, url: url, mediaInfo: mediaInfo))
}
// Init player with prev queue
public func startSavedAudioWithPrevQueue(withSavedUrl url: URL, mediaInfo: SALockScreenInfo? = nil) {
startSavedAudio(withSavedUrl: url, mediaInfo: mediaInfo)
addSavedToPlayedQueue(withSavedUrl: url, mediaInfo: mediaInfo)
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First thanks for great library.
I could not find way to force play next or previous track place in queue. may be you can suggest some way.
The text was updated successfully, but these errors were encountered: