-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathAudioQueueRemoteExample.swift
171 lines (150 loc) · 6.23 KB
/
AudioQueueRemoteExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// QueueAudioRemoteCommandFactoryExample.swift
// ModernAVPlayer_Example
//
// Created by ankierman on 23/05/2020.
// Copyright © 2020 CocoaPods. All rights reserved.
//
import Foundation
import MediaPlayer
import ModernAVPlayer
final class AudioQueueRemoteExample {
// MARK: - Output
/// Return all factory commands
///
var defaultCommands: [ModernAVPlayerRemoteCommand] {
return [playCommand,
pauseCommand,
stopCommand,
togglePlayPauseCommand,
prevTrackCommand,
nextTrackCommand
]
}
// MARK: - Inputs
private unowned let player: ModernAVPlayerExposable
private let commandCenter: MPRemoteCommandCenter
private let library: AudioQueueLibrary
var selectedMediaIndexChanged: ((String) -> Void)?
// MARK: - Init
init(player: ModernAVPlayerExposable,
commandCenter: MPRemoteCommandCenter = MPRemoteCommandCenter.shared(),
library: AudioQueueLibrary) {
self.player = player
self.commandCenter = commandCenter
self.library = library
}
// MARK: - Playback Commands
/// Default play command
///
lazy var playCommand: ModernAVPlayerRemoteCommand = {
let command = commandCenter.playCommand
let isEnabled: (MediaType) -> Bool = { _ in true }
let handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus = { _ in
guard let media = self.player.currentMedia
else { return .noSuchContent }
guard case let .stream(isLive) = media.type, isLive
else { self.player.play(); return .success }
self.player.load(media: media, autostart: true, position: nil)
return .success
}
command.addTarget(handler: handler)
return ModernAVPlayerRemoteCommand(reference: command,
debugDescription: "Play",
isEnabled: isEnabled)
}()
/// Default toggle play pause command
///
lazy var togglePlayPauseCommand: ModernAVPlayerRemoteCommand = {
let command = commandCenter.togglePlayPauseCommand
let isEnabled: (MediaType) -> Bool = { _ in true }
let handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus = { _ in
switch self.player.state {
case .buffering, .loading, .playing:
self.player.pause()
case .failed, .initialization, .waitingForNetwork:
return .noSuchContent
case .loaded, .paused, .stopped:
self.player.play()
}
return .success
}
command.addTarget(handler: handler)
return ModernAVPlayerRemoteCommand(reference: command,
debugDescription: "Toggle play/pause",
isEnabled: isEnabled)
}()
/// Default pause command
///
lazy var pauseCommand: ModernAVPlayerRemoteCommand = {
let command = commandCenter.pauseCommand
let isEnabled: (MediaType) -> Bool = {
guard case .stream(let isLive) = $0, isLive else { return true }
return false
}
let handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus = { _ in
self.player.pause()
return .success
}
command.addTarget(handler: handler)
return ModernAVPlayerRemoteCommand(reference: command,
debugDescription: "Pause",
isEnabled: isEnabled)
}()
/// Default stop command
///
lazy var stopCommand: ModernAVPlayerRemoteCommand = {
let command = commandCenter.stopCommand
let isEnabled: (MediaType) -> Bool = {
guard case .stream(let isLive) = $0, isLive else { return false }
return true
}
let handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus = { _ in
self.player.stop()
return .success
}
command.addTarget(handler: handler)
return ModernAVPlayerRemoteCommand(reference: command,
debugDescription: "Stop",
isEnabled: isEnabled)
}()
// MARK: - Navigating between tracks
/// Previous Track Command
/// Not enabled yet
///
lazy var prevTrackCommand: ModernAVPlayerRemoteCommand = {
let command = commandCenter.previousTrackCommand
let isEnabled: (MediaType) -> Bool = { _ in true }
let handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus = { [weak self] _ in
guard let self = self else { return .commandFailed }
self.library.changeMedia(userAction: .prevTrack)
let media = self.library.selectedMedia
self.player.load(media: media, autostart: true, position: nil)
self.selectedMediaIndexChanged?(self.library.index.description)
return .success
}
command.addTarget(handler: handler)
return ModernAVPlayerRemoteCommand(reference: command,
debugDescription: "Prev track",
isEnabled: isEnabled)
}()
/// Next Track Command
/// Not enabled yet
///
lazy var nextTrackCommand: ModernAVPlayerRemoteCommand = {
let command = commandCenter.nextTrackCommand
let isEnabled: (MediaType) -> Bool = { _ in true }
let handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus = { [weak self] _ in
guard let self = self else { return .commandFailed }
self.library.changeMedia(userAction: .nextTrack)
let media = self.library.selectedMedia
self.player.load(media: media, autostart: true, position: nil)
self.selectedMediaIndexChanged?(self.library.index.description)
return .success
}
command.addTarget(handler: handler)
return ModernAVPlayerRemoteCommand(reference: command,
debugDescription: "Next track",
isEnabled: isEnabled)
}()
}