Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Add removeAllItems() #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions Source/Jukebox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ extension Jukebox {
queuedItems.remove(at: index)
}
}

/**
Removes all items from the play queue.

*/
public func removeAllItems(){
queuedItems.removeAll(keepingCapacity: true)
}

}


Expand Down Expand Up @@ -442,35 +451,15 @@ open class Jukebox: NSObject, JukeboxItemDelegate {

fileprivate func configureAudioSession() throws {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setMode(AVAudioSessionModeDefault)
try AVAudioSession.sharedInstance().setActive(true)
}

fileprivate func configureObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(Jukebox.handleStall), name: NSNotification.Name.AVPlayerItemPlaybackStalled, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleAudioSessionInterruption), name: NSNotification.Name.AVAudioSessionInterruption, object: AVAudioSession.sharedInstance())
}

// MARK:- Notifications -

func handleAudioSessionInterruption(_ notification : Notification) {
guard let userInfo = notification.userInfo as? [String: AnyObject] else { return }
guard let rawInterruptionType = userInfo[AVAudioSessionInterruptionTypeKey] as? NSNumber else { return }
guard let interruptionType = AVAudioSessionInterruptionType(rawValue: rawInterruptionType.uintValue) else { return }

switch interruptionType {
case .began: //interruption started
self.pause()
case .ended: //interruption ended
if let rawInterruptionOption = userInfo[AVAudioSessionInterruptionOptionKey] as? NSNumber {
let interruptionOption = AVAudioSessionInterruptionOptions(rawValue: rawInterruptionOption.uintValue)
if interruptionOption == AVAudioSessionInterruptionOptions.shouldResume {
self.resumePlayback()
}
}
}
}

func handleStall() {
player?.pause()
player?.play()
Expand Down