Skip to content

Commit

Permalink
Add event for queue index changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Aug 12, 2021
1 parent aeef676 commit 92053a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion SwiftAudioEx/Classes/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension AudioPlayer {
public typealias UpdateDurationEventData = (Double)
public typealias MetadataEventData = ([AVMetadataItem])
public typealias DidRecreateAVPlayerEventData = ()
public typealias QueueIndexEventData = (previousIndex: Int?, newIndex: Int?)

public struct EventHolder {

Expand Down Expand Up @@ -70,7 +71,13 @@ extension AudioPlayer {
- Note: It can be necessary to set the AVAudioSession's category again when this event is emitted.
*/
public let didRecreateAVPlayer: AudioPlayer.Event<()> = AudioPlayer.Event()


/**
Emitted when a new track starts and the queue index changes.
- Important: Remember to dispatch to the main queue if any UI is updated in the event handler.
- Note: It is only fired for instances of a QueuedAudioPlayer.
*/
public let queueIndex: AudioPlayer.Event<QueueIndexEventData> = AudioPlayer.Event()
}

public typealias EventClosure<EventData> = (EventData) -> Void
Expand Down
2 changes: 1 addition & 1 deletion SwiftAudioEx/Classes/QueueManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

protocol QueueManagerDelegate: class {
protocol QueueManagerDelegate: AnyObject {
func onReceivedFirstItem()
func onCurrentIndexChanged(oldIndex: Int, newIndex: Int)
}
Expand Down
20 changes: 19 additions & 1 deletion SwiftAudioEx/Classes/QueuedAudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import MediaPlayer
/**
An audio player that can keep track of a queue of AudioItems.
*/
public class QueuedAudioPlayer: AudioPlayer {
public class QueuedAudioPlayer: AudioPlayer, QueueManagerDelegate {

let queueManager: QueueManager = QueueManager<AudioItem>()

public init() {
super.init()
queueManager.delegate = self
}

/// The repeat mode for the queue player.
public var repeatMode: RepeatMode = .off

Expand All @@ -34,6 +39,7 @@ public class QueuedAudioPlayer: AudioPlayer {
*/
public override func stop() {
super.stop()
self.event.queueIndex.emit(data: (currentIndex, nil))
}

override func reset() {
Expand Down Expand Up @@ -199,4 +205,16 @@ public class QueuedAudioPlayer: AudioPlayer {
} catch { /* TODO: handle possible errors from load */ }
}
}

// MARK: - QueueManagerDelegate

func onCurrentIndexChanged(oldIndex: Int, newIndex: Int) {
// if _currentItem is nil, then this was triggered by a reset. ignore.
if _currentItem == nil { return }
self.event.queueIndex.emit(data: (oldIndex, newIndex))
}

func onReceivedFirstItem() {
self.event.queueIndex.emit(data: (nil, 0))
}
}

0 comments on commit 92053a2

Please sign in to comment.