处理外屏播放的问题
This commit is contained in:
parent
c38e6a78de
commit
96d030977a
@ -98,9 +98,6 @@ class ZZHCustomPlayer: UIView {
|
|||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(notification_PlayerEndTime(notification:)), name: AVPlayerItem.didPlayToEndTimeNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(notification_PlayerEndTime(notification:)), name: AVPlayerItem.didPlayToEndTimeNotification, object: nil)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let playerWidth = KScreenWidth
|
let playerWidth = KScreenWidth
|
||||||
let playerHeight = 200
|
let playerHeight = 200
|
||||||
playerLayerBgView = UIView()
|
playerLayerBgView = UIView()
|
||||||
@ -140,13 +137,11 @@ class ZZHCustomPlayer: UIView {
|
|||||||
|
|
||||||
//通过slider进行seek播放时间点
|
//通过slider进行seek播放时间点
|
||||||
func manualToSeekPlay(value:Float,isMoving:Bool){
|
func manualToSeekPlay(value:Float,isMoving:Bool){
|
||||||
|
|
||||||
if isMoving {
|
if isMoving {
|
||||||
self.play(false)
|
// self.play(false)
|
||||||
print("此处已暂停播放,那么后面希望不要出现 sta.....> 的打印信息")
|
self.avPlayer?.pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Task {
|
|
||||||
let totalSec = CMTimeGetSeconds((self.avPlayer?.currentItem?.duration)!)
|
let totalSec = CMTimeGetSeconds((self.avPlayer?.currentItem?.duration)!)
|
||||||
var atSec = Float(totalSec) * value
|
var atSec = Float(totalSec) * value
|
||||||
var timeScale:CMTimeScale? = self.avPlayer?.currentItem?.duration.timescale
|
var timeScale:CMTimeScale? = self.avPlayer?.currentItem?.duration.timescale
|
||||||
@ -157,53 +152,17 @@ class ZZHCustomPlayer: UIView {
|
|||||||
timeScale = CMTimeScale(1)
|
timeScale = CMTimeScale(1)
|
||||||
}
|
}
|
||||||
let ct = CMTime(value:CMTimeValue(atSec) , timescale: timeScale!)
|
let ct = CMTime(value:CMTimeValue(atSec) , timescale: timeScale!)
|
||||||
// CustomPlayerShareSemaphore.wait()
|
|
||||||
// CustomPlayerShareRecursiveLock.lock()
|
|
||||||
// self.quickLoadAReaderWhenSeek(startCT: ct)
|
|
||||||
// CustomPlayerShareRecursiveLock.unlock()
|
|
||||||
// CustomPlayerShareSemaphore.signal()
|
|
||||||
print("正在seek.....\(ct) ismainthread:\(Thread.isMainThread)")
|
|
||||||
// DispatchQueue.main.async {
|
|
||||||
playerIsSeeking = true
|
playerIsSeeking = true
|
||||||
self.avPlayer?.seek(to: ct,toleranceBefore:.zero,toleranceAfter: .zero, completionHandler: { finished in
|
self.avPlayer?.seek(to: ct,toleranceBefore:.zero,toleranceAfter: .zero)
|
||||||
if finished {
|
|
||||||
if(!isMoving) {//结束拖动时,再进行播放
|
if(!isMoving) {//结束拖动时,再进行播放
|
||||||
// CustomPlayerShareSemaphore.wait()
|
// self.play(true)
|
||||||
// CustomPlayerShareRecursiveLock.lock()
|
self.avPlayer?.play()
|
||||||
// self.quickLoadAReaderWhenSeek(startCT: ct)
|
|
||||||
// CustomPlayerShareRecursiveLock.unlock()
|
|
||||||
// CustomPlayerShareSemaphore.signal()
|
|
||||||
self.play(true)
|
|
||||||
self.playerIsSeeking = false
|
self.playerIsSeeking = false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//检查视频的当前播放时间是否落后于readeroutput的copynextSampleBuffer的对应time,如果是,则seek到copynextSampleBuffer下一个时间点
|
|
||||||
func checkFBTime(ct_Buffer:CMTime?) {
|
|
||||||
return //该方法目前有问题,行不通,会闪屏,并且会卡顿
|
|
||||||
// guard let ct_Buffer,let playerCurrCT = self.avPlayer?.currentTime() else {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// print("checkFBTime......call CMTimeGetSeconds:\(CMTimeGetSeconds(playerCurrCT))")
|
|
||||||
// if ct_Buffer > playerCurrCT{
|
|
||||||
// print("in checkFBTime......call\nct_Buffer: \(ct_Buffer),\nplayerCurrCT: \(playerCurrCT)")
|
|
||||||
// let nct = CMTime(value: ct_Buffer.value+20, timescale: ct_Buffer.timescale)
|
|
||||||
// self.avPlayer?.seek(to: nct,toleranceBefore:.zero,toleranceAfter: .zero, completionHandler: {finished in
|
|
||||||
// print("checkFBTime seek result:\(finished)")
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//初始化播放器信息
|
//初始化播放器信息
|
||||||
func setUPPlayer() {
|
func setUPPlayer() {
|
||||||
@ -224,34 +183,20 @@ class ZZHCustomPlayer: UIView {
|
|||||||
|
|
||||||
//改变avplayer所在的avplayerLayer视图曾层:用户外接屏幕发声改变时
|
//改变avplayer所在的avplayerLayer视图曾层:用户外接屏幕发声改变时
|
||||||
func moveToOtherPlayerLayer(destLayer:AVPlayerLayer) {
|
func moveToOtherPlayerLayer(destLayer:AVPlayerLayer) {
|
||||||
print("moveToOtherPlayerLayer 准备暂停播放")
|
|
||||||
self.play(false)
|
self.play(false)
|
||||||
print("avPlayerLayer 准备移除player")
|
|
||||||
self.avPlayerLayer?.player = nil
|
self.avPlayerLayer?.player = nil
|
||||||
print("avPlayerLayer 已经移除player")
|
|
||||||
destLayer.player = self.avPlayer
|
destLayer.player = self.avPlayer
|
||||||
if assetTrack != nil {
|
|
||||||
quickLoadAReaderWhenSeek(startCT: (self.avPlayer?.currentTime())!)
|
|
||||||
}
|
|
||||||
|
|
||||||
print("外屏destLayer 已添加player")
|
|
||||||
self.play(true)
|
self.play(true)
|
||||||
print("moveToOtherPlayerLayer 恢复播放play")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//当外接屏幕断开时,恢复到原来的layer层上
|
//当外接屏幕断开时,恢复到原来的layer层上
|
||||||
func recoveryPlayerLayer(otherLayer:AVPlayerLayer) {
|
func recoveryPlayerLayer(otherLayer:AVPlayerLayer) {
|
||||||
print("recoveryPlayerLayer 准备暂停播放")
|
|
||||||
self.play(false)
|
self.play(false)
|
||||||
print("otherLayer 准备移除player")
|
|
||||||
otherLayer.player = nil
|
otherLayer.player = nil
|
||||||
print("otherLayer 已经移除player")
|
|
||||||
avPlayerLayer?.player = self.avPlayer
|
avPlayerLayer?.player = self.avPlayer
|
||||||
quickLoadAReaderWhenSeek(startCT: (self.avPlayer?.currentTime())!)
|
|
||||||
print("手机屏幕avPlayerLayer 已添加player")
|
|
||||||
self.play(true)
|
self.play(true)
|
||||||
print("recoveryPlayerLayer 恢复播放play")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//暂停或者播放
|
//暂停或者播放
|
||||||
@objc func playOrPause(sender:UIButton) {
|
@objc func playOrPause(sender:UIButton) {
|
||||||
if prePlayingState == false {//播放
|
if prePlayingState == false {//播放
|
||||||
@ -312,7 +257,6 @@ class ZZHCustomPlayer: UIView {
|
|||||||
|
|
||||||
//监听播放完毕
|
//监听播放完毕
|
||||||
@objc func notification_PlayerEndTime(notification:Notification){
|
@objc func notification_PlayerEndTime(notification:Notification){
|
||||||
print("PlayerEndTime....")
|
|
||||||
avPlayer?.seek(to: .zero,toleranceBefore: .zero,toleranceAfter: .zero,completionHandler: {[weak self] finish in
|
avPlayer?.seek(to: .zero,toleranceBefore: .zero,toleranceAfter: .zero,completionHandler: {[weak self] finish in
|
||||||
self?.reLoadPlay()
|
self?.reLoadPlay()
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user