-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathSimpleAudioQueueVC.swift
More file actions
132 lines (104 loc) · 3.46 KB
/
SimpleAudioQueueVC.swift
File metadata and controls
132 lines (104 loc) · 3.46 KB
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
//
// SimpleAudioQueueVC.swift
// ModernAVPlayer_Example
//
// Created by ankierman on 22/05/2020.
// Copyright © 2020 CocoaPods. All rights reserved.
//
import ModernAVPlayer
import RxSwift
import RxCocoa
import UIKit
final class SimpleAudioQueueVC: UIViewController {
// MARK: - Inputs
private let player: ModernAVPlayer = {
let conf = PlayerConfigurationExample()
return ModernAVPlayer(config: conf, loggerDomains: [.error, .unavailableCommand])
}()
private let library = ModernAudioQueueLibrary()
private lazy var remote: AudioQueueRemoteExample = {
return AudioQueueRemoteExample(player: self.player, library: self.library)
}()
private var playNext: Disposable?
private let disposeBag = DisposeBag()
// MARK: - Interface Buidler
@IBOutlet weak private var mediaLabel: UILabel!
@IBOutlet weak private var stateLabel: UILabel!
@IBOutlet weak private var timingLabel: UILabel!
@IBOutlet weak private var itemDuration: UILabel!
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
title = "Simple Audio Queue"
setupRxLabels()
player.remoteCommands = remote.defaultCommands
setupRemoteCallback()
loadMedia()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playNext?.dispose()
}
private func setupRxLabels() {
player.rx.state
.map({ "State: " + $0.description })
.bind(to: stateLabel.rx.text)
.disposed(by: disposeBag)
player.rx.currentTime
.map({ "Current time: " + $0.description })
.bind(to: timingLabel.rx.text)
.disposed(by: disposeBag)
player.rx.itemDuration
.map({
if let txt = $0?.description { return "Duration: " + txt }
else { return "Duration is nil" }
})
.bind(to: itemDuration.rx.text)
.disposed(by: disposeBag)
}
@IBAction func setupPlayNext(_ sender: UISwitch) {
guard sender.isOn else {
playNext?.dispose()
playNext = nil
return
}
playNext = player.rx.itemPlayToEndTime
.subscribe(onNext: { [weak self] _ in
DispatchQueue.main.async {
self?.loadMedia(userAction: .nextTrack)
}
})
}
private func setupMediaIndexLabel(index: String) {
mediaLabel.text = "Media index: " + index
}
private func setupRemoteCallback() {
remote.selectedMediaIndexChanged = { [weak self] index in
self?.setupMediaIndexLabel(index: index)
}
}
// MARK: - Commands
@IBAction func play(_ sender: UIButton) {
player.play()
}
@IBAction func pause(_ sender: UIButton) {
player.pause()
}
@IBAction func stop(_ sender: Any) {
player.stop()
}
@IBAction func prevSeek(_ sender: UIButton) {
loadMedia(userAction: .prevTrack)
}
@IBAction func nextSeek(_ sender: UIButton) {
loadMedia(userAction: .nextTrack)
}
private func loadMedia(userAction: ModernAudioQueueLibrary.UserAction? = nil) {
if let action = userAction {
library.changeMedia(userAction: action)
}
setupMediaIndexLabel(index: library.index.description)
let media = library.selectedMedia
player.load(media: media, autostart: true)
}
}