Skip to content
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

Open
maasim94 opened this issue Sep 8, 2021 · 1 comment
Open

Play Next or Previous from Queue #153

maasim94 opened this issue Sep 8, 2021 · 1 comment
Assignees

Comments

@maasim94
Copy link

maasim94 commented Sep 8, 2021

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.

@tanhakabir tanhakabir self-assigned this Sep 10, 2021
@NuclleaR
Copy link

NuclleaR commented Sep 2, 2022

Hello!

I also had such question.
here the solution that I've used

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants