使用AVPlayer自定義支持全屏的播放器(五)——Swift重構(gòu)版本

前言

很早之前開(kāi)源了一個(gè)簡(jiǎn)單的視頻播放器,由于年久失修,效果慘目忍睹,最近特意花時(shí)間對(duì)其進(jìn)行了深度重構(gòu)。舊版本后期不再維護(hù),新版本使用Swift實(shí)現(xiàn),后續(xù)會(huì)增加更多功能。不想看文字的請(qǐng)自行下載代碼------>>>CLPlayer

舊版本 VS 重構(gòu)版本

1.新版本使用Swift,舊版本使用Objective-C

2.新版本采用自定義轉(zhuǎn)場(chǎng)實(shí)現(xiàn)全屏,舊版本使用旋轉(zhuǎn)屏幕

3.新版本不需要手動(dòng)銷毀播放器

4.新版本修復(fù)了老版本遺留bug

5.新版本降低了代碼耦合性

6.新版本增加了倍數(shù)播放,切換填充模式

7.新版本提供更豐富的API

8.新版本適配了iPhone X

9.新版本移除了狀態(tài)欄相關(guān)配置

效果

效果圖
全屏
控制面板
UITableView
4.png

功能

  • 支持全屏模式、小屏模式
  • 支持跟隨手機(jī)自動(dòng)旋轉(zhuǎn)
  • 支持本地視頻、網(wǎng)絡(luò)URL
  • 支持UITableView
  • 支持UICollectionView
  • 支持手勢(shì)改變屏幕的亮度(屏幕左半邊)
  • 支持手勢(shì)改變音量大小(屏幕右半邊)
  • 支持拖動(dòng)UISlider快進(jìn)快退
  • 支持iPhone X留海屏
  • 支持倍速播放(0.5X、1.0X、1.25X、1.5X、1.75X、2X
  • 支持動(dòng)態(tài)改變播放器的填充模式(適應(yīng)、拉伸、填充
  • 支持cocoapods

接入指南

項(xiàng)目必須支持全屏,建議將屏幕支持方向交由當(dāng)前顯示的控制器自行管理。

項(xiàng)目支持全屏方案

1.先勾選支持方向,只保留portrait,保證APP啟動(dòng)不會(huì)橫屏

image.png

2.AppDelegate中重寫func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {}方法

func application(_: UIApplication, supportedInterfaceOrientationsFor _: UIWindow?) -> UIInterfaceOrientationMask {
        return .allButUpsideDown
}

3.在父類中重寫屏幕控制相關(guān)方法

UITabBarController

// 是否支持自動(dòng)轉(zhuǎn)屏
override var shouldAutorotate: Bool {
    guard let navigationController = selectedViewController as? UINavigationController else { return selectedViewController?.shouldAutorotate ?? false }
    return navigationController.topViewController?.shouldAutorotate ?? false
}

// 支持哪些屏幕方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    guard let navigationController = selectedViewController as? UINavigationController else { return selectedViewController?.supportedInterfaceOrientations ?? .portrait }
    return navigationController.topViewController?.supportedInterfaceOrientations ?? .portrait
}

// 默認(rèn)的屏幕方向(當(dāng)前ViewController必須是通過(guò)模態(tài)出來(lái)的UIViewController(模態(tài)帶導(dǎo)航的無(wú)效)方式展現(xiàn)出來(lái)的,才會(huì)調(diào)用這個(gè)方法)
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    guard let navigationController = selectedViewController as? UINavigationController else { return selectedViewController?.preferredInterfaceOrientationForPresentation ?? .portrait }
    return navigationController.topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
}

UINavigationController

// 是否支持自動(dòng)轉(zhuǎn)屏
override var shouldAutorotate: Bool {
    return topViewController?.shouldAutorotate ?? false
}

// 支持哪些屏幕方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return topViewController?.supportedInterfaceOrientations ?? .portrait
}

// 默認(rèn)的屏幕方向(當(dāng)前ViewController必須是通過(guò)模態(tài)出來(lái)的UIViewController(模態(tài)帶導(dǎo)航的無(wú)效)方式展現(xiàn)出來(lái)的,才會(huì)調(diào)用這個(gè)方法)
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
}

UIViewController

// 是否支持自動(dòng)轉(zhuǎn)屏
override var shouldAutorotate: Bool {
    return false
}

// 支持哪些屏幕方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

// 默認(rèn)的屏幕方向(當(dāng)前ViewController必須是通過(guò)模態(tài)出來(lái)的UIViewController(模態(tài)帶導(dǎo)航的無(wú)效)方式展現(xiàn)出來(lái)的,才會(huì)調(diào)用這個(gè)方法)
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}

4.部分頁(yè)面需要支持多方向

在對(duì)應(yīng)控制器中重寫以下方法

override var shouldAutorotate: Bool {
    return true
}

// 支持哪些屏幕方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .allButUpsideDown
}

基礎(chǔ)配置

public struct CLPlayerConfigure {
    /// 頂部工具條隱藏風(fēng)格
    public enum CLPlayerTopBarHiddenStyle {
        /// 小屏和全屏都不隱藏
        case never
        /// 小屏和全屏都隱藏
        case always
        /// 小屏隱藏,全屏不隱藏
        case onlySmall
    }

    /// 自動(dòng)旋轉(zhuǎn)
    public var isAutoRotate = true
    /// 手勢(shì)控制
    public var isGestureInteractionEnabled = true
    /// 是否顯示更多面板
    public var isShowMorePanel = true
    /// 頂部工具條隱藏風(fēng)格
    public var topBarHiddenStyle: CLPlayerTopBarHiddenStyle = .onlySmall
    /// 工具條自動(dòng)消失時(shí)間
    public var autoFadeOut: TimeInterval = 5
    /// 默認(rèn)拉伸方式
    public var videoGravity: AVLayerVideoGravity = .resizeAspectFill
    /// 頂部工具條背景顏色
    public var topToobarBackgroundColor: UIColor = .black.withAlphaComponent(0.6)
    /// 底部工具條背景顏色
    public var bottomToolbarBackgroundColor: UIColor = .black.withAlphaComponent(0.6)
    /// 進(jìn)度條背景顏色
    public var progressBackgroundColor: UIColor = .white.withAlphaComponent(0.35)
    /// 緩沖條緩沖進(jìn)度顏色
    public var progressBufferColor: UIColor = .white.withAlphaComponent(0.5)
    /// 進(jìn)度條播放完成顏色
    public var progressFinishedColor: UIColor = .white
    /// 轉(zhuǎn)子背景顏色
    public var loadingBackgroundColor: UIColor = .white
    /// 返回按鈕圖片
    public var backImage: UIImage?
    /// 更多按鈕圖片
    public var moreImage: UIImage?
    /// 播放按鈕圖片
    public var playImage: UIImage?
    /// 暫停按鈕圖片
    public var pauseImage: UIImage?
    /// 進(jìn)度滑塊圖片
    public var sliderImage: UIImage?
    /// 最大化按鈕圖片
    public var maxImage: UIImage?
    /// 最小化按鈕圖片
    public var minImage: UIImage?
    /// 封面圖片
    public var maskImage: UIImage?
}

總結(jié)

本次重構(gòu)為Swift第一版,后續(xù)會(huì)持續(xù)更新,定制化開(kāi)發(fā)請(qǐng)自行參考CLPlayer修改 , 如果喜歡,歡迎star。

參考資料

  1. iOS播放器全屏方案

  2. iOS狀態(tài)欄

  3. iOS播放器全屏旋轉(zhuǎn)實(shí)現(xiàn)

  4. iOS橫豎屏旋轉(zhuǎn)解決方案 - Swift

  5. iOS視頻旋轉(zhuǎn)探究

  6. iOS屏幕旋轉(zhuǎn)的解決方案

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容