Skip to content

Commit

Permalink
Finish up with auto-play
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-dev committed Jan 20, 2021
1 parent b75761f commit c646dde
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Example/MediaSlideshow/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ViewController: UIViewController {
let kingfisherSource = [KingfisherSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080")!, KingfisherSource(urlString: "https://images.unsplash.com/photo-1447746249824-4be4e1b76d66?w=1080")!, KingfisherSource(urlString: "https://images.unsplash.com/photo-1463595373836-6e0b0a8ee322?w=1080")!]
let videoSource = AVSource(
url: URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4")!,
onAppear: .paused)
onAppear: .play)

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
15 changes: 8 additions & 7 deletions MediaSlideshow/Source/AV/AVPlayerSlide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ public protocol AVPlayerSlideDelegate: AnyObject {
func currentThumbnail(_ slide: AVPlayerSlide) -> UIImage?
func slideDidAppear(_ slide: AVPlayerSlide)
func slideDidDisappear(_ slide: AVPlayerSlide)
func slideDidSingleTap(_ slide: AVPlayerSlide)
}

public class AVPlayerSlide: UIView, MediaSlideshowSlide {
weak var delegate: AVPlayerSlideDelegate?

private let playerController: AVPlayerViewController
public let playerController: AVPlayerViewController
private let transitionView: UIImageView

public init(
Expand All @@ -34,7 +33,9 @@ public class AVPlayerSlide: UIView, MediaSlideshowSlide {
transitionView.isHidden = true
embed(transitionView)
embed(playerController.view)
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didSingleTap)))
if playerController.showsPlaybackControls {
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didSingleTap)))
}
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -57,7 +58,9 @@ public class AVPlayerSlide: UIView, MediaSlideshowSlide {
}
}

public func willBeRemoved() {}
public func willBeRemoved() {
playerController.player?.pause()
}

public func loadMedia() {}

Expand All @@ -79,7 +82,5 @@ public class AVPlayerSlide: UIView, MediaSlideshowSlide {
}

@objc
private func didSingleTap() {
delegate?.slideDidSingleTap(self)
}
private func didSingleTap() {}
}
47 changes: 19 additions & 28 deletions MediaSlideshow/Source/AV/AVSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import AVKit
import Foundation

open class AVSource: NSObject, MediaSource {
public enum Playback {
case play(muted: Bool)
public enum Playback: Equatable {
case play // will be muted when playback controls are hidden
case paused
}
private let onAppear: Playback
private let asset: AVAsset
public private(set) lazy var item = AVPlayerItem(asset: asset)
public private(set) lazy var player = AVPlayer(playerItem: item)
private lazy var item = AVPlayerItem(asset: asset)
private lazy var player = AVPlayer(playerItem: item)

public init(asset: AVAsset, onAppear: Playback) {
self.asset = asset
Expand All @@ -37,26 +37,20 @@ open class AVSource: NSObject, MediaSource {
open func slide(in slideshow: MediaSlideshow) -> MediaSlideshowSlide {
let playerController = AVPlayerViewController()
playerController.player = player
switch onAppear {
case .paused:
playerController.showsPlaybackControls = true
let overlay = StandardAVSlideOverlayView(
item: item,
player: player,
playView: nil,
pauseView: nil,
activityView: slideshow.activityIndicator?.create())
playerController.contentOverlayView?.embed(overlay)
case .play:
playerController.showsPlaybackControls = false
let overlay = StandardAVSlideOverlayView(
item: item,
player: player,
playView: AVSlidePlayingOverlayView(),
pauseView: AVSlidePausedOverlayView(),
activityView: slideshow.activityIndicator?.create())
playerController.contentOverlayView?.embed(overlay)
playerController.showsPlaybackControls = onAppear == .paused || slideshow.zoomEnabled
var playView: AVSlidePlayingOverlayView?
var pauseView: AVSlidePausedOverlayView?
if !playerController.showsPlaybackControls {
playView = AVSlidePlayingOverlayView()
pauseView = AVSlidePausedOverlayView()
}
let overlay = StandardAVSlideOverlayView(
item: item,
player: player,
playView: playView,
pauseView: pauseView,
activityView: slideshow.activityIndicator?.create())
playerController.contentOverlayView?.embed(overlay)
let slide = AVPlayerSlide(
playerController: playerController,
mediaContentMode: slideshow.contentScaleMode)
Expand All @@ -71,14 +65,11 @@ open class AVSource: NSObject, MediaSource {
}

extension AVSource: AVPlayerSlideDelegate {
public func slideDidSingleTap(_ slide: AVPlayerSlide) {
}

open func slideDidAppear(_ slide: AVPlayerSlide) {
switch onAppear {
case .play(let muted):
case .play:
player.play()
player.isMuted = muted
player.isMuted = !slide.playerController.showsPlaybackControls
case .paused:
player.pause()
}
Expand Down
5 changes: 3 additions & 2 deletions MediaSlideshow/Source/MediaSlideshow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ open class MediaSlideshow: UIView {
}
scrollViewPage = 0
loadMedia(for: scrollViewPage)
if slides.count > scrollViewPage {
slides[scrollViewPage].didAppear()
if !slides.isEmpty {
delegate?.mediaSlideshow?(self, didChangeCurrentPageTo: 0)
slides[0].didAppear()
}
}

Expand Down

0 comments on commit c646dde

Please sign in to comment.