Compare commits
3 Commits
1.0.0-rc.2
...
combine
| Author | SHA1 | Date | |
|---|---|---|---|
| e6ca8c9b1e | |||
| 83318d3ff0 | |||
| 3c5f858a94 |
@@ -63,6 +63,18 @@ class MyCustomViewController: UIViewController {
|
||||
}
|
||||
```
|
||||
|
||||
If you want to use the [Combine](https://developer.apple.com/documentation/combine) framework for events, each event in the `AudioPlayer` has an `EventPublisher` that can be subscribed to:
|
||||
```swift
|
||||
let audioPlayer = AudioPlayer()
|
||||
audioPlayer.event.stateChange.publisher.subscribe(subscriber: someSubscriber)
|
||||
|
||||
/// Using a Sink
|
||||
audioPlayer.event.stateChange.publisher.sink { state in
|
||||
/// Handle state change here.
|
||||
}
|
||||
```
|
||||
**Important**: This requires iOS version equal to or later than 13.0. If an application needs to support older iOS versions it is recommended to use the regular `Event.addListener(listener:)` method.
|
||||
|
||||
#### QueuedAudioPlayer
|
||||
The `QueuedAudioPlayer` is a subclass of `AudioPlayer` that maintains a queue of audio tracks.
|
||||
```swift
|
||||
|
||||
@@ -10,18 +10,20 @@ import Foundation
|
||||
|
||||
public struct APError {
|
||||
|
||||
enum LoadError: Error {
|
||||
public enum LoadError: Error {
|
||||
case invalidSourceUrl(String)
|
||||
}
|
||||
|
||||
enum PlaybackError: Error {
|
||||
public enum PlaybackError: Error {
|
||||
case noLoadedItem
|
||||
}
|
||||
|
||||
enum QueueError: Error {
|
||||
public enum QueueError: Error {
|
||||
case noPreviousItem
|
||||
case noNextItem
|
||||
case invalidIndex(index: Int, message: String)
|
||||
}
|
||||
|
||||
public enum EventError: Error {}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
extension AudioPlayer {
|
||||
|
||||
@@ -69,7 +70,7 @@ extension AudioPlayer {
|
||||
|
||||
class Invoker<EventData> {
|
||||
|
||||
// Signals false if the listener object is nil
|
||||
/// Signals false if the listener object is nil. If `false` is signaled, the invoker should not be retained.
|
||||
let invoke: (EventData) -> Bool
|
||||
weak var listener: AnyObject?
|
||||
|
||||
@@ -114,7 +115,7 @@ extension AudioPlayer {
|
||||
self.invokersSemaphore.signal()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func emit(data: EventData) {
|
||||
eventQueue.async {
|
||||
self.invokersSemaphore.wait()
|
||||
@@ -125,6 +126,34 @@ extension AudioPlayer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
The publisher for this event. Use this for subscription through the Combine framework.
|
||||
*/
|
||||
@available(iOS 13.0, *)
|
||||
public lazy var publisher: EventPublisher<EventData> = EventPublisher(event: self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@available(iOS 13.0, *)
|
||||
extension AudioPlayer {
|
||||
|
||||
public class EventPublisher<EventData>: Publisher {
|
||||
|
||||
public typealias Output = EventData
|
||||
public typealias Failure = APError.EventError
|
||||
|
||||
private weak var event: Event<EventData>?
|
||||
|
||||
init(event: Event<EventData>) {
|
||||
self.event = event
|
||||
}
|
||||
|
||||
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
|
||||
event?.addListener(self) { (data) in
|
||||
let _ = subscriber.receive(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user