From 17afadbb6be972bf1aa1d284513a7b384e63f950 Mon Sep 17 00:00:00 2001 From: Nam Kennic Date: Tue, 16 May 2023 23:15:46 +0700 Subject: [PATCH 1/3] reponds to AVPlayerItemDidPlayToEndTime event to currentItem only --- MMPlayerView.podspec | 2 +- .../Classes/Swift/MMPlayerLayer.swift | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/MMPlayerView.podspec b/MMPlayerView.podspec index 66b864e..f14240b 100644 --- a/MMPlayerView.podspec +++ b/MMPlayerView.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'MMPlayerView' - s.version = '6.0.2' + s.version = '6.0.3' s.summary = 'Custom Video Player view' # This description is used to generate tags and improve search results. diff --git a/MMPlayerView/Classes/Swift/MMPlayerLayer.swift b/MMPlayerView/Classes/Swift/MMPlayerLayer.swift index 3d6c60c..3832c3a 100644 --- a/MMPlayerView/Classes/Swift/MMPlayerLayer.swift +++ b/MMPlayerView/Classes/Swift/MMPlayerLayer.swift @@ -604,22 +604,27 @@ extension MMPlayerLayer { self?.isBackgroundPause = false }) - NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil, using: { [weak self] (_) in - - if self?.repeatWhenEnd == true { - self?.player?.seek(to: CMTime.zero) - self?.player?.play() - } else if let s = self?.currentPlayStatus { - switch s { + NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil, using: { [weak self] (notification) in + guard let self else { return } + + if repeatWhenEnd == true { + guard let player else { return } + guard let playItem = notification.object as? AVPlayerItem else { return } + guard playItem == player.currentItem else { return } + player.seek(to: .zero) + player.play() + } else { + switch currentPlayStatus { case .playing, .pause: - if let u = self?.asset?.url { - self?.cahce.removeCache(key: u) + if let u = asset?.url { + cahce.removeCache(key: u) } - self?.currentPlayStatus = .end + currentPlayStatus = .end default: break } } }) + self.addObserver(self, forKeyPath: "videoRect", options: [.new, .old], context: nil) bgView.addObserver(self, forKeyPath: "frame", options: [.new, .old], context: nil) bgView.addObserver(self, forKeyPath: "bounds", options: [.new, .old], context: nil) From 61c8fca744bed7a206b85ad8083021126b688a60 Mon Sep 17 00:00:00 2001 From: Nam Kennic Date: Mon, 6 May 2024 22:32:13 +0700 Subject: [PATCH 2/3] no auto resume when resume from background --- MMPlayerView/Classes/Swift/MMPlayerLayer.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/MMPlayerView/Classes/Swift/MMPlayerLayer.swift b/MMPlayerView/Classes/Swift/MMPlayerLayer.swift index 3832c3a..b0f8167 100644 --- a/MMPlayerView/Classes/Swift/MMPlayerLayer.swift +++ b/MMPlayerView/Classes/Swift/MMPlayerLayer.swift @@ -222,6 +222,8 @@ public class MMPlayerLayer: AVPlayerLayer { public var isShrink: Bool { return shrinkControl.isShrink } + + var autoResume = true // MARK: - Private Parameter lazy var subtitleView = MMSubtitleView() @@ -425,10 +427,18 @@ extension MMPlayerLayer { self.initStatus() self.asset = nil } + + public func pause() { + player?.pause() + currentPlayStatus = .pause + autoResume = false + } + /** Start player to play video */ public func resume() { + autoResume = true switch self.currentPlayStatus { case .playing , .pause: if (self.player?.currentItem?.asset as? AVURLAsset)?.url == self.asset?.url { @@ -598,7 +608,7 @@ extension MMPlayerLayer { }) NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil, using: { [weak self] (nitification) in - if self?.isBackgroundPause == false { + if self?.isBackgroundPause == false && self?.autoResume == true { self?.player?.play() } self?.isBackgroundPause = false From a707b2dbd568167bc7dcaadec0ea80318043050a Mon Sep 17 00:00:00 2001 From: Nam Kennic Date: Mon, 6 May 2024 22:32:38 +0700 Subject: [PATCH 3/3] no message --- MMPlayerView.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MMPlayerView.podspec b/MMPlayerView.podspec index f14240b..88864b1 100644 --- a/MMPlayerView.podspec +++ b/MMPlayerView.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'MMPlayerView' - s.version = '6.0.3' + s.version = '6.0.4' s.summary = 'Custom Video Player view' # This description is used to generate tags and improve search results.