Skip to content

Commit

Permalink
新增自定义遮罩
Browse files Browse the repository at this point in the history
  • Loading branch information
JmoVxia committed Dec 12, 2022
1 parent ad50a58 commit 61d3bf9
Show file tree
Hide file tree
Showing 57 changed files with 787 additions and 3,856 deletions.
3 changes: 1 addition & 2 deletions CLPlayer.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Pod::Spec.new do |s|

s.name = 'CLPlayer'
s.version = '2.0.3'
s.version = '2.0.4'
s.summary = 'Swift版自定义AVPlayer'
s.description = <<-DESC
CLPlayer是基于系统AVPlayer封装的视频播放器.
* 支持Autolayout、Frame.
* 支持UITableView、UICollectionView.
* 支持亮度、音量调节.
* 支持进度调节.
Expand Down
11 changes: 8 additions & 3 deletions CLPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
11A3DE9060FD8CD4516A2F1D /* libPods-CLPlayer.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C4745977FFF79AB78FC9B13E /* libPods-CLPlayer.a */; };
5B1F5BBA2943200B00D554D7 /* CLPlaceholderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B1F5BB92943200B00D554D7 /* CLPlaceholderView.swift */; };
5B2D28E32729446800E85A53 /* CLRotateAnimationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2D28E22729446800E85A53 /* CLRotateAnimationView.swift */; };
5B2D28E52729463800E85A53 /* CLPlayer.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 5B2D28E42729463800E85A53 /* CLPlayer.bundle */; };
5B2D28E72729468000E85A53 /* CLImageHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2D28E62729468000E85A53 /* CLImageHelper.swift */; };
Expand Down Expand Up @@ -53,6 +54,7 @@
/* Begin PBXFileReference section */
2A4B8371167B1ECA19BBA36A /* Pods-CLPlayer.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CLPlayer.debug.xcconfig"; path = "Target Support Files/Pods-CLPlayer/Pods-CLPlayer.debug.xcconfig"; sourceTree = "<group>"; };
2FB9093F4F6D6B5B3B0E722C /* Pods-CLPlayer.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CLPlayer.release.xcconfig"; path = "Target Support Files/Pods-CLPlayer/Pods-CLPlayer.release.xcconfig"; sourceTree = "<group>"; };
5B1F5BB92943200B00D554D7 /* CLPlaceholderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLPlaceholderView.swift; sourceTree = "<group>"; };
5B2D28E22729446800E85A53 /* CLRotateAnimationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLRotateAnimationView.swift; sourceTree = "<group>"; };
5B2D28E42729463800E85A53 /* CLPlayer.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = CLPlayer.bundle; sourceTree = "<group>"; };
5B2D28E62729468000E85A53 /* CLImageHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLImageHelper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -185,10 +187,10 @@
5BA39B162727D147005059F5 /* CLPlayer */ = {
isa = PBXGroup;
children = (
5B2D28E62729468000E85A53 /* CLImageHelper.swift */,
5BE1F3382727E5780090F6C8 /* CLPlayer.swift */,
5B33B4DD2769EAA20086B402 /* CLPlayerConfigure.swift */,
5B33B4DF2769F2990086B402 /* CLPlayerDelegate.swift */,
5B2D28E62729468000E85A53 /* CLImageHelper.swift */,
5BFADCB9276893B600A596A6 /* CLGCDTimer.swift */,
5BFADCB72768936C00A596A6 /* CLPlayerContentView */,
5BFADCB82768937D00A596A6 /* CLFullScreenController */,
Expand Down Expand Up @@ -347,6 +349,7 @@
isa = PBXGroup;
children = (
5BFADCB5276888B400A596A6 /* CLViewController.swift */,
5B1F5BB92943200B00D554D7 /* CLPlaceholderView.swift */,
);
path = CLViewController;
sourceTree = "<group>";
Expand Down Expand Up @@ -519,6 +522,7 @@
};
5B918B752727D7E2008355E5 /* SwiftFormat */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -568,6 +572,7 @@
5B72C195272A39B1004B9AEA /* CLSlider.swift in Sources */,
5BC4103B276B19C300DFE028 /* CLCollectionViewCell.swift in Sources */,
5BFADCAC2767415200A596A6 /* CLPlayerContentPanelHeadView.swift in Sources */,
5B1F5BBA2943200B00D554D7 /* CLPlaceholderView.swift in Sources */,
5BE1F3342727E3800090F6C8 /* UIImage+Extension.swift in Sources */,
5B2D28E72729468000E85A53 /* CLImageHelper.swift in Sources */,
5B37DEE72727DBA5002A377A /* CLHomeController.swift in Sources */,
Expand Down Expand Up @@ -741,7 +746,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = JmoVxia.CLPlayer;
PRODUCT_BUNDLE_IDENTIFIER = com.JmoVxia.CLPlayer;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -771,7 +776,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = JmoVxia.CLPlayer;
PRODUCT_BUNDLE_IDENTIFIER = com.JmoVxia.CLPlayer;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down
2 changes: 1 addition & 1 deletion CLPlayer/CLGCDTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit

class CLGCDTimer: NSObject {
typealias actionBlock = ((NSInteger) -> Void)
typealias actionBlock = (NSInteger) -> Void
/// 执行时间
private var interval: TimeInterval!
/// 延迟时间
Expand Down
4 changes: 2 additions & 2 deletions CLPlayer/CLImageHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import UIKit

class CLImageHelper: NSObject {
static func imageWithName(_ name: String) -> UIImage? {
public class CLImageHelper: NSObject {
public static func imageWithName(_ name: String) -> UIImage? {
let filePath = Bundle(for: classForCoder()).resourcePath! + "/CLPlayer.bundle"
let bundle = Bundle(path: filePath)
let scale = max(min(Int(UIScreen.main.scale), 2), 3)
Expand Down
91 changes: 42 additions & 49 deletions CLPlayer/CLPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ extension CLPlayer {
}

public class CLPlayer: UIView {
public init(frame: CGRect = .zero, configure: ((inout CLPlayerConfigure) -> Void)? = nil) {
public init(frame: CGRect = .zero, config: ((inout CLPlayerConfigure) -> Void)? = nil) {
super.init(frame: frame)
configure?(&config)
updateConfig()
config?(&self.config)
initUI()
makeConstraints()
(layer as? AVPlayerLayer)?.videoGravity = self.config.videoGravity
}

@available(*, unavailable)
Expand All @@ -32,9 +32,7 @@ public class CLPlayer: UIView {
}

deinit {
if config.isAutoRotate {
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
}
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
print("CLPlayer deinit")
Expand Down Expand Up @@ -78,8 +76,6 @@ public class CLPlayer: UIView {

private var player: AVPlayer?

private var playerLayer: AVPlayerLayer?

private var playerItem: AVPlayerItem? {
didSet {
guard playerItem != oldValue else { return }
Expand Down Expand Up @@ -130,13 +126,6 @@ public class CLPlayer: UIView {
}
}

public private(set) var videoGravity: AVLayerVideoGravity = .resizeAspectFill {
didSet {
guard videoGravity != oldValue else { return }
playerLayer?.videoGravity = videoGravity
}
}

public var isFullScreen: Bool {
return contentView.screenState == .fullScreen
}
Expand Down Expand Up @@ -181,9 +170,13 @@ public class CLPlayer: UIView {
}
playerItem = AVPlayerItem(asset: .init(url: url))
player = AVPlayer(playerItem: playerItem)
playerLayer = layer as? AVPlayerLayer
playerLayer?.videoGravity = videoGravity
playerLayer?.player = player
(layer as? AVPlayerLayer)?.player = player
}
}

public weak var placeholder: UIView? {
didSet {
contentView.placeholderView = placeholder
}
}

Expand All @@ -204,25 +197,19 @@ private extension CLPlayer {
func initUI() {
backgroundColor = .black
addSubview(contentView)
if config.isAutoRotate {
if !UIDevice.current.isGeneratingDeviceOrientationNotifications {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
}
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
}
NotificationCenter.default.addObserver(self, selector: #selector(appDidEnterBackground), name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appDidEnterPlayground), name: UIApplication.didBecomeActiveNotification, object: nil)
if !UIDevice.current.isGeneratingDeviceOrientationNotifications {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
}
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
}

func makeConstraints() {
contentView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}

func updateConfig() {
videoGravity = config.videoGravity
}
}

// MARK: - JmoVxia---objc
Expand All @@ -239,6 +226,10 @@ private extension CLPlayer {
}

func deviceOrientationDidChange() {
guard config.rotateStyle != .none else { return }
if config.rotateStyle == .small, isFullScreen { return }
if config.rotateStyle == .fullScreen, !isFullScreen { return }

switch UIDevice.current.orientation {
case .portrait:
dismiss()
Expand Down Expand Up @@ -362,6 +353,7 @@ private extension CLPlayer {
self.contentView.screenState = .small
})
fullScreenController = nil
UIViewController.attemptRotationToDeviceOrientation()
}

func presentWithOrientation(_ orientation: CLAnimationTransitioning.CLAnimationOrientation) {
Expand All @@ -378,6 +370,7 @@ private extension CLPlayer {
fullScreenController?.modalPresentationStyle = .fullScreen
rootViewController.present(fullScreenController!, animated: true, completion: {
self.contentView.screenState = .fullScreen
UIViewController.attemptRotationToDeviceOrientation()
})
}
}
Expand All @@ -390,6 +383,7 @@ public extension CLPlayer {
guard !isUserPause else { return }
guard let playerItem = playerItem else { return }
guard playerItem.status == .readyToPlay else {
contentView.playState = .waiting
waitReadyToPlayState = .play
return
}
Expand Down Expand Up @@ -430,7 +424,6 @@ public extension CLPlayer {

playerItem = nil
player = nil
playerLayer = nil

isUserPause = false

Expand Down Expand Up @@ -462,17 +455,34 @@ extension CLPlayer: UIViewControllerTransitioningDelegate {
// MARK: - JmoVxia---CLPlayerContentViewDelegate

extension CLPlayer: CLPlayerContentViewDelegate {
func sliderTouchBegan(_: CLSlider, in _: CLPlayerContentView) {
func contentView(_ contentView: CLPlayerContentView, didClickPlayButton isPlay: Bool) {
isUserPause = isPlay
isPlay ? pause() : play()
}

func contentView(_ contentView: CLPlayerContentView, didClickFullButton isFull: Bool) {
isFull ? dismiss() : presentWithOrientation(.fullRight)
}

func contentView(_ contentView: CLPlayerContentView, didChangeRate rate: Float) {
self.rate = rate
}

func contentView(_ contentView: CLPlayerContentView, didChangeVideoGravity videoGravity: AVLayerVideoGravity) {
(layer as? AVPlayerLayer)?.videoGravity = videoGravity
}

func contentView(_ contentView: CLPlayerContentView, sliderTouchBegan slider: CLSlider) {
pause()
}

func sliderValueChanged(_ slider: CLSlider, in _: CLPlayerContentView) {
func contentView(_ contentView: CLPlayerContentView, sliderValueChanged slider: CLSlider) {
currentDuration = totalDuration * TimeInterval(slider.value)
let dragedCMTime = CMTimeMake(value: Int64(ceil(currentDuration)), timescale: 1)
player?.seek(to: dragedCMTime, toleranceBefore: .zero, toleranceAfter: .zero)
}

func sliderTouchEnded(_ slider: CLSlider, in _: CLPlayerContentView) {
func contentView(_ contentView: CLPlayerContentView, sliderTouchEnded slider: CLSlider) {
guard let playerItem = playerItem else { return }
if slider.value == 1 {
didPlaybackEnds()
Expand All @@ -495,21 +505,4 @@ extension CLPlayer: CLPlayerContentViewDelegate {
self.delegate?.didClickBackButton(in: self)
}
}

func didClickPlayButton(isPlay: Bool, in _: CLPlayerContentView) {
isUserPause = isPlay
isPlay ? pause() : play()
}

func didClickFullButton(isFull: Bool, in _: CLPlayerContentView) {
isFull ? dismiss() : presentWithOrientation(.fullRight)
}

func didChangeRate(_ rate: Float, in _: CLPlayerContentView) {
self.rate = rate
}

func didChangeVideoGravity(_ videoGravity: AVLayerVideoGravity, in _: CLPlayerContentView) {
self.videoGravity = videoGravity
}
}
Loading

0 comments on commit 61d3bf9

Please sign in to comment.