From 9db893a224913b6e12bf75291329e072ec516d48 Mon Sep 17 00:00:00 2001 From: Gorhom Omar Date: Wed, 14 Jun 2017 02:28:29 +0800 Subject: [PATCH] #support fullscreen mode ! --- Project/FullScreenPlayer.swift | 54 ++++++++++++++++++++++++ Project/Player.xcodeproj/project.pbxproj | 4 ++ Project/Player/ViewController.swift | 18 ++++++++ Sources/Player.swift | 16 ++++++- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Project/FullScreenPlayer.swift diff --git a/Project/FullScreenPlayer.swift b/Project/FullScreenPlayer.swift new file mode 100644 index 0000000..1785f41 --- /dev/null +++ b/Project/FullScreenPlayer.swift @@ -0,0 +1,54 @@ +// +// FullScreenPlayer.swift +// Player +// +// Created by Mohammed Omar on 14/06/2017. +// Copyright © 2017 Patrick Piemonte. All rights reserved. +// + +import Foundation +import AVFoundation +import AVKit + +class FullScreenPlayer: AVPlayerViewController{ + + // MARK:- Variables + fileprivate var _supportedInterfaceOrientations: UIInterfaceOrientationMask! + + open weak var playbackDelegate: PlayerPlaybackDelegate? + + // MARK:- Initiation + required convenience init(playbackDelegate: PlayerPlaybackDelegate?, supportedInterfaceOrientations: UIInterfaceOrientationMask) { + self.init() + + self.playbackDelegate = playbackDelegate + self._supportedInterfaceOrientations = supportedInterfaceOrientations + } + + + // MARK:- Overrides + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + player?.play() + } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + playbackDelegate?.playerEnterFullScreen() + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + playbackDelegate?.playerExitFullScreen() + } + + override var supportedInterfaceOrientations: UIInterfaceOrientationMask{ + return _supportedInterfaceOrientations + } + + override var shouldAutorotate: Bool { + return true + } +} diff --git a/Project/Player.xcodeproj/project.pbxproj b/Project/Player.xcodeproj/project.pbxproj index ff30b25..1da360e 100644 --- a/Project/Player.xcodeproj/project.pbxproj +++ b/Project/Player.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 065A0FAD1A25C8E2005BA7BC /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 065A0FAB1A25C8E2005BA7BC /* LaunchScreen.xib */; }; 065A0FC61A25C94B005BA7BC /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 065A0FC51A25C94B005BA7BC /* Player.swift */; }; 06C0905F1E038F250038CA25 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 065A0FC51A25C94B005BA7BC /* Player.swift */; }; + F87326A51EF057C7008D2915 /* FullScreenPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F87326A41EF057C7008D2915 /* FullScreenPlayer.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -39,6 +40,7 @@ 065A0FC51A25C94B005BA7BC /* Player.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Player.swift; path = ../Sources/Player.swift; sourceTree = ""; }; 06C6A5891A89E952009DC6EA /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = SOURCE_ROOT; }; 06DE65951E182C5A00C064DE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Framework/Info.plist; sourceTree = SOURCE_ROOT; }; + F87326A41EF057C7008D2915 /* FullScreenPlayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FullScreenPlayer.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -152,6 +154,7 @@ isa = PBXGroup; children = ( 065A0FC51A25C94B005BA7BC /* Player.swift */, + F87326A41EF057C7008D2915 /* FullScreenPlayer.swift */, ); name = Player; sourceTree = ""; @@ -318,6 +321,7 @@ buildActionMask = 2147483647; files = ( 065A0FA51A25C8E2005BA7BC /* ViewController.swift in Sources */, + F87326A51EF057C7008D2915 /* FullScreenPlayer.swift in Sources */, 065A0FC61A25C94B005BA7BC /* Player.swift in Sources */, 065A0FA31A25C8E2005BA7BC /* AppDelegate.swift in Sources */, ); diff --git a/Project/Player/ViewController.swift b/Project/Player/ViewController.swift index 94c7642..af67b11 100644 --- a/Project/Player/ViewController.swift +++ b/Project/Player/ViewController.swift @@ -32,6 +32,7 @@ class ViewController: UIViewController { fileprivate var player = Player() + // MARK: object lifecycle deinit { self.player.willMove(toParentViewController: self) @@ -61,6 +62,13 @@ class ViewController: UIViewController { let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGestureRecognizer(_:))) tapGestureRecognizer.numberOfTapsRequired = 1 self.player.view.addGestureRecognizer(tapGestureRecognizer) + + + let fullscreenButton = UIButton(frame: CGRect(x: 0, y: view.frame.height - 50, width: view.frame.width, height: 50)) + fullscreenButton.setTitle("Fullscreen", for: .normal) + fullscreenButton.setTitleColor(UIColor.white, for: .normal) + fullscreenButton.addTarget(self, action: #selector(tapOnFullscreen), for: .touchUpInside) + self.view.addSubview(fullscreenButton) } override func viewDidAppear(_ animated: Bool) { @@ -68,6 +76,10 @@ class ViewController: UIViewController { self.player.playFromBeginning() } + + func tapOnFullscreen(){ + player.enterFullScreen() + } } // MARK: - UIGestureRecognizer @@ -104,6 +116,7 @@ extension ViewController:PlayerDelegate { } func playerPlaybackStateDidChange(_ player: Player) { + print(player.playbackState) } func playerBufferingStateDidChange(_ player: Player) { @@ -130,5 +143,10 @@ extension ViewController:PlayerPlaybackDelegate { func playerPlaybackWillLoop(_ player: Player) { } + func playerEnterFullScreen() { + } + + func playerExitFullScreen() { + } } diff --git a/Sources/Player.swift b/Sources/Player.swift index faa9a59..999d23b 100644 --- a/Sources/Player.swift +++ b/Sources/Player.swift @@ -118,6 +118,9 @@ public protocol PlayerPlaybackDelegate: NSObjectProtocol { func playerPlaybackWillStartFromBeginning(_ player: Player) func playerPlaybackDidEnd(_ player: Player) func playerPlaybackWillLoop(_ player: Player) + + func playerEnterFullScreen() + func playerExitFullScreen() } // MARK: - Player @@ -280,6 +283,7 @@ open class Player: UIViewController { } } internal var _avplayer: AVPlayer + internal weak var _avFullScreenPlayer: FullScreenPlayer? internal var _playerItem: AVPlayerItem? internal var _timeObserver: Any? @@ -343,7 +347,7 @@ open class Player: UIViewController { open override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) - if self.playbackState == .playing { + if self.playbackState == .playing && _avFullScreenPlayer == nil { self.pause() } } @@ -438,6 +442,16 @@ open class Player: UIViewController { open func playerLayer() -> AVPlayerLayer? { return self._playerView.playerLayer } + + /// Enter full screen mode + open func enterFullScreen(supportedInterfaceOrientations: UIInterfaceOrientationMask = .landscape){ + let _avFullScreenPlayer = FullScreenPlayer(playbackDelegate: playbackDelegate, supportedInterfaceOrientations: supportedInterfaceOrientations) + _avFullScreenPlayer.player = _avplayer + + present(_avFullScreenPlayer, animated: true, completion: nil) + + self._avFullScreenPlayer = _avFullScreenPlayer + } } // MARK: - loading funcs