Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reponds to AVPlayerItemDidPlayToEndTime event to currentItem only #122

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MMPlayerView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MMPlayerView'
s.version = '6.0.2'
s.version = '6.0.4'
s.summary = 'Custom Video Player view'

# This description is used to generate tags and improve search results.
Expand Down
37 changes: 26 additions & 11 deletions MMPlayerView/Classes/Swift/MMPlayerLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -598,28 +608,33 @@ 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
})

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)
Expand Down