diff --git a/Example/MediaSlideshow/ViewController.swift b/Example/MediaSlideshow/ViewController.swift index 1c2fcde..7d7b248 100644 --- a/Example/MediaSlideshow/ViewController.swift +++ b/Example/MediaSlideshow/ViewController.swift @@ -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() diff --git a/MediaSlideshow/Source/AV/AVPlayerSlide.swift b/MediaSlideshow/Source/AV/AVPlayerSlide.swift index 9c8c175..8e46bb2 100644 --- a/MediaSlideshow/Source/AV/AVPlayerSlide.swift +++ b/MediaSlideshow/Source/AV/AVPlayerSlide.swift @@ -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( @@ -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) { @@ -57,7 +58,9 @@ public class AVPlayerSlide: UIView, MediaSlideshowSlide { } } - public func willBeRemoved() {} + public func willBeRemoved() { + playerController.player?.pause() + } public func loadMedia() {} @@ -79,7 +82,5 @@ public class AVPlayerSlide: UIView, MediaSlideshowSlide { } @objc - private func didSingleTap() { - delegate?.slideDidSingleTap(self) - } + private func didSingleTap() {} } diff --git a/MediaSlideshow/Source/AV/AVSource.swift b/MediaSlideshow/Source/AV/AVSource.swift index f6bb6df..e77d19c 100644 --- a/MediaSlideshow/Source/AV/AVSource.swift +++ b/MediaSlideshow/Source/AV/AVSource.swift @@ -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 @@ -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) @@ -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() } diff --git a/MediaSlideshow/Source/MediaSlideshow.swift b/MediaSlideshow/Source/MediaSlideshow.swift index c58e43b..a8c89a0 100644 --- a/MediaSlideshow/Source/MediaSlideshow.swift +++ b/MediaSlideshow/Source/MediaSlideshow.swift @@ -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() } }