-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativePlayerView.swift
295 lines (239 loc) · 11.9 KB
/
NativePlayerView.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// NativePlayerView.swift
// SmartStream
//
// Created by Hugo Nguyen on 3/14/16.
// Copyright © 2016 SmartStream. All rights reserved.
//
import UIKit
import AVFoundation
class NativePlayerView: NSObject {
let playerId: String
let playerType: PlayerType
weak var playerDelegate: SmartuPlayerDelegate?
weak var playerContainerView: UIView?
private var nativePlayerView: UIView!
private var nativePlayerLayer: AVPlayerLayer!
private var nativePlayerOverlay: UIView!
private let myContext: UnsafeMutablePointer<Void> = nil
private var nativePlayer: AVPlayer!
private var timeObserver: AnyObject?
private var currBounds: CGRect
private var currItem: ChannelItem?
private var isPlaying = false
private var playEnabled = false
private var didStart = false
init(playerId: String, containerView: UIView?) {
debugPrint("[NATIVEPLAYER] init()")
self.playerId = playerId
self.playerType = .Native
self.playerContainerView = containerView
nativePlayer = AVQueuePlayer()
nativePlayerView = UIView(frame: containerView!.bounds)
nativePlayerView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
nativePlayerView.backgroundColor = UIColor.clearColor()
nativePlayerLayer = AVPlayerLayer(player: self.nativePlayer)
nativePlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect
nativePlayerLayer.frame = containerView!.bounds
nativePlayerView.layer.addSublayer(nativePlayerLayer)
nativePlayerLayer.needsDisplayOnBoundsChange = true
nativePlayerView.layer.needsDisplayOnBoundsChange = true
containerView!.addSubview(nativePlayerView)
nativePlayerOverlay = UIView(frame: containerView!.bounds)
nativePlayerOverlay.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
nativePlayerOverlay.backgroundColor = UIColor.blackColor()
nativePlayerOverlay.alpha = 0.0
nativePlayerOverlay.userInteractionEnabled = false
containerView!.addSubview(nativePlayerOverlay)
currBounds = containerView!.bounds
super.init()
nativePlayer.addObserver(self, forKeyPath: "status", options: [.New], context: self.myContext)
nativePlayer.addObserver(self, forKeyPath: "currentItem", options: [.New], context: self.myContext)
nativePlayer.addObserver(self, forKeyPath: "duration", options: [.New], context: self.myContext)
nativePlayer.addObserver(self, forKeyPath: "loadedTimeRanges", options: [.New], context: self.myContext)
nativePlayer.addObserver(self, forKeyPath: "presentationSize", options: [.New], context: self.myContext)
nativePlayer.addObserver(self, forKeyPath: "error", options: [.New], context: self.myContext)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NativePlayerView.nativePlayerDidFinishPlaying(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
// an observer for every second playback
timeObserver = nativePlayer!.addPeriodicTimeObserverForInterval(CMTimeMake(1,1), queue: nil, usingBlock: {[weak self] (time: CMTime) -> Void in
if let strongSelf = self {
if strongSelf.nativePlayer != nil && strongSelf.nativePlayer!.currentItem != nil {
if !strongSelf.playEnabled {
strongSelf.pauseItem()
strongSelf.nativePlayerOverlay.alpha = 1.0
return
}
if !strongSelf.isPlaying {
strongSelf.isPlaying = true
strongSelf.show(nil)
}
let currentSecond = CMTimeGetSeconds(strongSelf.nativePlayer!.currentItem!.currentTime())
let totalDuration = CMTimeGetSeconds(strongSelf.nativePlayer!.currentItem!.duration)
if !strongSelf.didStart {
strongSelf.didStart = true
strongSelf.playerDelegate?.playbackStatus(strongSelf.playerId, playerType: strongSelf.playerType, status: .DidStart, progress: 0, totalDuration: Double.NaN)
} else {
strongSelf.playerDelegate?.playbackStatus(strongSelf.playerId, playerType: strongSelf.playerType, status: .Playing, progress: Double(currentSecond), totalDuration: totalDuration)
}
if totalDuration == totalDuration && Int64(totalDuration) - Int64(currentSecond) == bufferTimeConstant {
strongSelf.playerDelegate?.playbackStatus(strongSelf.playerId, playerType: strongSelf.playerType, status: .WillEnd, progress: 0.0, totalDuration: 0.0)
NSTimer.scheduledTimerWithTimeInterval(2.0, target: strongSelf, selector: #selector(strongSelf.endItem), userInfo: nil, repeats: false)
}
}
}
})
}
deinit {
debugPrint("[NATIVEPLAYER] deinit()")
if timeObserver != nil {
nativePlayer.removeTimeObserver(timeObserver!)
}
nativePlayer.removeObserver(self, forKeyPath: "status")
nativePlayer.removeObserver(self, forKeyPath: "currentItem")
nativePlayer.removeObserver(self, forKeyPath: "duration")
nativePlayer.removeObserver(self, forKeyPath: "loadedTimeRanges")
nativePlayer.removeObserver(self, forKeyPath: "presentationSize")
nativePlayer.removeObserver(self, forKeyPath: "error")
NSNotificationCenter.defaultCenter().removeObserver(self)
nativePlayerView.layer.sublayers = nil
nativePlayerView.removeFromSuperview()
nativePlayerOverlay.removeFromSuperview()
}
}
extension NativePlayerView: SmartuPlayer {
func bufferItem(item: ChannelItem!) {
debugPrint("[NATIVEPLAYER] bufferItem(): extractor = \(item.extractor!); vid = \(item.native_id!)")
playEnabled = false
self.currItem = (item.copy() as! ChannelItem)
let asset = AVURLAsset(URL: NSURL(string: item.url!)!, options: nil)
asset.loadValuesAsynchronouslyForKeys(["playable"], completionHandler: { () -> Void in
dispatch_async(dispatch_get_main_queue(),{
self.nativePlayer.replaceCurrentItemWithPlayerItem(AVPlayerItem(asset: asset))
if !item.seekToSeconds.isNaN {
self.nativePlayer.seekToTime(CMTimeMakeWithSeconds(Float64(item.seekToSeconds), 1))
}
})
})
}
func startItem(item: ChannelItem!) {
debugPrint("[NATIVEPLAYER] startItem(); vid = \(item.native_id!)")
self.currItem = (item.copy() as! ChannelItem)
let asset = AVURLAsset(URL: NSURL(string: item.url!)!, options: nil)
asset.loadValuesAsynchronouslyForKeys(["playable"], completionHandler: { () -> Void in
dispatch_async(dispatch_get_main_queue(),{
self.nativePlayer.replaceCurrentItemWithPlayerItem(AVPlayerItem(asset: asset))
if !item.seekToSeconds.isNaN {
self.nativePlayer.seekToTime(CMTimeMakeWithSeconds(Float64(item.seekToSeconds), 1))
}
self.playItem()
})
})
}
func playItem() {
playEnabled = true
debugPrint("[NATIVEPLAYER] playItem(); vid = \(currItem!.native_id!)")
self.nativePlayer.play()
}
func pauseItem() {
debugPrint("[NATIVEPLAYER] pauseItem()")
self.nativePlayer.pause()
}
func stopItem() {
debugPrint("[NATIVEPLAYER] stopItem()")
self.currItem = nil
self.isPlaying = false
self.nativePlayer.pause()
self.nativePlayer.replaceCurrentItemWithPlayerItem(nil)
self.hide(0.0)
}
func nextItem() {
debugPrint("[NATIVEPLAYER] nextItem()")
//stopItem()
}
func resetBounds(bounds: CGRect) {
if currBounds == bounds {return}
currBounds = bounds
debugPrint("[NATIVEPLAYER] resetBounds()")
self.nativePlayerLayer.frame = bounds
}
func getItem() -> ChannelItem? {
return currItem
}
func getPlayerId() -> String {
return playerId
}
func getPlayerViews() -> [UIView] {
return [nativePlayerView, nativePlayerOverlay]
}
func show(duration: NSTimeInterval?) {
dispatch_async(dispatch_get_main_queue(),{
debugPrint("[NATIVEPLAYER] fades in native player")
var du = fadeInTimeConstant
if duration != nil {
du = duration!
}
self.nativePlayerOverlay.alpha = 1.0
self.playerContainerView?.bringSubviewToFront(self.nativePlayerView)
self.playerContainerView?.bringSubviewToFront(self.nativePlayerOverlay)
UIView.animateWithDuration(du) { () -> Void in
self.nativePlayerOverlay.alpha = 0.0
}
})
}
func hide(duration: NSTimeInterval?) {
dispatch_async(dispatch_get_main_queue(),{
debugPrint("[NATIVEPLAYER] fades out native player")
var du = fadeOutTimeConstant
if duration != nil {
du = duration!
}
self.nativePlayerOverlay.alpha = 0.0
UIView.animateWithDuration(du) { () -> Void in
self.nativePlayerOverlay.alpha = 1.0
}
NSTimer.scheduledTimerWithTimeInterval(du, target: self, selector: #selector(self.removeViews), userInfo: nil, repeats: false)
})
}
func removeViews() {
self.nativePlayerView.removeFromSuperview()
self.nativePlayerOverlay.removeFromSuperview()
}
func endItem() {
hide(nil)
}
func nativePlayerDidFinishPlaying(notification: NSNotification) {
if isPlaying && nativePlayer.rate != 0 && nativePlayer.error == nil {
isPlaying = false
playerDelegate?.playbackStatus(self.playerId, playerType: self.playerType, status: .DidEnd, progress: 0.0, totalDuration: 0.0)
}
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if currItem == nil {return}
if context != myContext {return}
let nativePlayer = object as? AVPlayer
if (nativePlayer == nil || nativePlayer! != self.nativePlayer) {return}
if keyPath == "status" {
if self.nativePlayer.status == AVPlayerStatus.ReadyToPlay {
debugPrint("[NATIVEPLAYER] ready to play; vid = \(currItem!.native_id!); playEnabled = \(self.playEnabled)")
if self.playEnabled {
dispatch_async(dispatch_get_main_queue(),{
self.playItem()
})
}
} else if self.nativePlayer.status == AVPlayerStatus.Failed {
debugPrint("[NATIVEPLAYER] failed to play; vid = \(currItem!.native_id!)")
} else {
debugPrint("[NATIVEPLAYER] unhandled playerItem status \(self.nativePlayer.status)")
}
} else if keyPath == "currentItem" {
} else if keyPath == "duration" {
if let timeInterval = self.nativePlayer.currentItem?.duration {
let totalDuration = CMTimeGetSeconds(timeInterval)
debugPrint("totalDuration = \(totalDuration)")
}
} else if keyPath == "loadedTimeRanges" {
} else if keyPath == "presentationSize" {
} else if keyPath == "error" {
}
}
}