Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 31c3c728f6 | |||
| d6375d996d | |||
| 730629a07a | |||
| 0bb59cde4a | |||
| 661a7755df | |||
| 01702b41f1 |
@@ -28,13 +28,13 @@ class ViewController: UIViewController {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
audioPlayer.delegate = self
|
||||
audioPlayer.enableRemoteCommands([
|
||||
audioPlayer.remoteCommands = [
|
||||
.stop,
|
||||
.togglePlayPause,
|
||||
.skipForward(preferredIntervals: [30]),
|
||||
.skipBackward(preferredIntervals: [30]),
|
||||
.changePlaybackPosition
|
||||
])
|
||||
]
|
||||
try? audioSessionController.set(category: .playback)
|
||||
try? audioSessionController.activateSession()
|
||||
let image = #imageLiteral(resourceName: "cover")
|
||||
|
||||
@@ -56,20 +56,22 @@ If you want audio to continue playing when the app is inactive, remember to acti
|
||||
App Settings -> Capabilities -> Background Modes -> Check 'Audio, AirPlay, and Picture in Picture'.
|
||||
|
||||
### Now Playing Info
|
||||
The `AudioPlayer` will automatically update the `MPNowPlayingInfoCenter` with artist, title, album, artwork, time etc. if the passed in `AudioItem` supports this.
|
||||
The `AudioPlayer` will automatically update the `MPNowPlayingInfoCenter` with artist, title, album, artwork, time if the passed in `AudioItem` supports this.
|
||||
If you need to set additional properties for some items use `AudioPlayer.add(property:)`. Available properties can be found in `NowPlayingInfoProperty`.
|
||||
|
||||
### Remote Commands
|
||||
|
||||
The player will handle remote commands received from `MPRemoteCommandCenter`'s shared instance, enabled by:
|
||||
```swift
|
||||
audioPlayer.enable(commands: [
|
||||
audioPlayer.remoteCommands = [
|
||||
.play,
|
||||
.pause,
|
||||
.skipForward(intervals: [30]),
|
||||
.skipBackward(intervals: [30]),
|
||||
])
|
||||
]
|
||||
```
|
||||
|
||||
These commands will be activated for each `AudioItem`. If you need some audio items to have different commands, implement `RemoteCommandable`. These commands will override the commands found in `AudioPlayer.remoteCommands` so make sure to supply all commands you need for that particular `AudioItem`.
|
||||
|
||||
**Remember** to go to App Settings -> Capabilites -> Background Modes -> Check 'Remote notifications'
|
||||
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'SwiftAudio'
|
||||
s.version = '0.2.0'
|
||||
s.version = '0.2.1'
|
||||
s.summary = 'Easy audio streaming for iOS'
|
||||
|
||||
# This description is used to generate tags and improve search results.
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
public enum SourceType {
|
||||
case stream
|
||||
case file
|
||||
|
||||
@@ -28,16 +28,22 @@ public class AudioPlayer {
|
||||
|
||||
let wrapper: AVPlayerWrapper
|
||||
let nowPlayingInfoController: NowPlayingInfoController
|
||||
let remoteCommandController: RemoteCommandController
|
||||
|
||||
public weak var delegate: AudioPlayerDelegate?
|
||||
public var currentItem: AudioItem?
|
||||
public let remoteCommandController: RemoteCommandController
|
||||
|
||||
|
||||
/**
|
||||
Set this to false to disable automatic updating of now playing info for control center and lock screen.
|
||||
*/
|
||||
public var automaticallyUpdateNowPlayingInfo: Bool = true
|
||||
|
||||
/**
|
||||
Default remote commands to use for each playing item
|
||||
*/
|
||||
public var remoteCommands: [RemoteCommand] = []
|
||||
|
||||
// MARK: - Getters from AVPlayerWrapper
|
||||
|
||||
/**
|
||||
@@ -145,6 +151,7 @@ public class AudioPlayer {
|
||||
self.currentItem = item
|
||||
set(item: item)
|
||||
setArtwork(forItem: item)
|
||||
enableRemoteCommands(forItem: item)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,10 +196,20 @@ public class AudioPlayer {
|
||||
Set the remote commands that should be activated and handled.
|
||||
Calling this will disable all earlier enabled commands, so include all commands you need.
|
||||
*/
|
||||
public func enableRemoteCommands(_ commands: [RemoteCommand]) {
|
||||
func enableRemoteCommands(_ commands: [RemoteCommand]) {
|
||||
self.remoteCommandController.enable(commands: commands)
|
||||
}
|
||||
|
||||
func enableRemoteCommands(forItem item: AudioItem) {
|
||||
if let item = item as? RemoteCommandable {
|
||||
print("Enabling remote commands for item")
|
||||
self.enableRemoteCommands(item.getCommands())
|
||||
}
|
||||
else {
|
||||
self.enableRemoteCommands(remoteCommands)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - NowPlayingInfo
|
||||
|
||||
/**
|
||||
@@ -205,8 +222,13 @@ public class AudioPlayer {
|
||||
updatePlaybackValues()
|
||||
}
|
||||
|
||||
public func add(property: NowPlayingInfoKeyValue) {
|
||||
self.nowPlayingInfoController.set(keyValue: property)
|
||||
}
|
||||
|
||||
func set(item: AudioItem) {
|
||||
guard automaticallyUpdateNowPlayingInfo else { return }
|
||||
|
||||
nowPlayingInfoController.set(keyValues: [
|
||||
MediaItemProperty.artist(item.getArtist()),
|
||||
MediaItemProperty.title(item.getTitle()),
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
import Foundation
|
||||
import MediaPlayer
|
||||
|
||||
public protocol RemoteCommandable {
|
||||
func getCommands() -> [RemoteCommand]
|
||||
}
|
||||
|
||||
public class RemoteCommandController {
|
||||
|
||||
@@ -36,11 +39,13 @@ public class RemoteCommandController {
|
||||
}
|
||||
|
||||
private func enableCommand<Command: RemoteCommandProtocol>(_ command: Command) {
|
||||
center[keyPath: command.commandKeyPath].isEnabled = true
|
||||
center[keyPath: command.commandKeyPath].addTarget(handler: self[keyPath: command.handlerKeyPath])
|
||||
}
|
||||
|
||||
private func disableCommand<Command: RemoteCommandProtocol>(_ command: Command) {
|
||||
center[keyPath: command.commandKeyPath].removeTarget(self)
|
||||
center[keyPath: command.commandKeyPath].isEnabled = false
|
||||
center[keyPath: command.commandKeyPath].removeTarget(self[keyPath: command.handlerKeyPath])
|
||||
}
|
||||
|
||||
private func enable(command: RemoteCommand) {
|
||||
|
||||
Reference in New Issue
Block a user