Compare commits

...

3 Commits

Author SHA1 Message Date
David Chavez 68a15ab3a6 Release 0.13.1 2021-08-12 11:18:08 +02:00
David Chavez 92053a2bd0 Add event for queue index changes 2021-08-12 11:16:56 +02:00
David Chavez aeef676164 Persist rate accross player vs item 2021-08-12 11:16:46 +02:00
5 changed files with 44 additions and 7 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = 'SwiftAudioEx'
s.version = '0.13.0'
s.version = '0.13.1'
s.summary = 'Easy audio streaming for iOS'
s.description = <<-DESC
SwiftAudioEx is an audio player written in Swift, making it simpler to work with audio playback from streams and files.
+15 -3
View File
@@ -115,9 +115,17 @@ public class AudioPlayer: AVPlayerWrapperDelegate {
set { _wrapper.isMuted = newValue }
}
private var _rate: Float = 1.0
public var rate: Float {
get { return wrapper.rate }
set { _wrapper.rate = newValue }
get { return _rate }
set {
_rate = newValue
// Only set the rate on the wrapper if it is already playing.
if _wrapper.rate > 0 {
_wrapper.rate = newValue
}
}
}
// MARK: - Init
@@ -311,7 +319,11 @@ public class AudioPlayer: AVPlayerWrapperDelegate {
updateNowPlayingPlaybackValues()
}
setTimePitchingAlgorithmForCurrentItem()
case .playing, .paused:
case .playing:
// When a track starts playing, reset the rate to the stored rate
self.rate = _rate;
fallthrough
case .paused:
if (automaticallyUpdateNowPlayingInfo) {
updateNowPlayingPlaybackValues()
}
+8 -1
View File
@@ -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<QueueIndexEventData> = AudioPlayer.Event()
}
public typealias EventClosure<EventData> = (EventData) -> Void
+1 -1
View File
@@ -7,7 +7,7 @@
import Foundation
protocol QueueManagerDelegate: class {
protocol QueueManagerDelegate: AnyObject {
func onReceivedFirstItem()
func onCurrentIndexChanged(oldIndex: Int, newIndex: Int)
}
+19 -1
View File
@@ -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
@@ -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))
}
}