From 92053a2bd08110a5cac1c9b22180e7a6e5fbcaad Mon Sep 17 00:00:00 2001 From: David Chavez Date: Thu, 12 Aug 2021 11:16:56 +0200 Subject: [PATCH] Add event for queue index changes --- SwiftAudioEx/Classes/Event.swift | 9 ++++++++- SwiftAudioEx/Classes/QueueManager.swift | 2 +- SwiftAudioEx/Classes/QueuedAudioPlayer.swift | 20 +++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/SwiftAudioEx/Classes/Event.swift b/SwiftAudioEx/Classes/Event.swift index a927412..8f16858 100644 --- a/SwiftAudioEx/Classes/Event.swift +++ b/SwiftAudioEx/Classes/Event.swift @@ -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 { @@ -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 = AudioPlayer.Event() } public typealias EventClosure = (EventData) -> Void diff --git a/SwiftAudioEx/Classes/QueueManager.swift b/SwiftAudioEx/Classes/QueueManager.swift index 3816557..2c86b4d 100755 --- a/SwiftAudioEx/Classes/QueueManager.swift +++ b/SwiftAudioEx/Classes/QueueManager.swift @@ -7,7 +7,7 @@ import Foundation -protocol QueueManagerDelegate: class { +protocol QueueManagerDelegate: AnyObject { func onReceivedFirstItem() func onCurrentIndexChanged(oldIndex: Int, newIndex: Int) } diff --git a/SwiftAudioEx/Classes/QueuedAudioPlayer.swift b/SwiftAudioEx/Classes/QueuedAudioPlayer.swift index 08c607f..e8ccec2 100755 --- a/SwiftAudioEx/Classes/QueuedAudioPlayer.swift +++ b/SwiftAudioEx/Classes/QueuedAudioPlayer.swift @@ -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() + public init() { + super.init() + queueManager.delegate = self + } + /// The repeat mode for the queue player. public var repeatMode: RepeatMode = .off @@ -34,6 +39,7 @@ public class QueuedAudioPlayer: AudioPlayer { */ public override func stop() { super.stop() + self.event.queueIndex.emit(data: (currentIndex, nil)) } override func reset() { @@ -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)) + } }