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

#support fullscreen mode ! #153

Open
wants to merge 1 commit into
base: main
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
54 changes: 54 additions & 0 deletions Project/FullScreenPlayer.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 4 additions & 0 deletions Project/Player.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -39,6 +40,7 @@
065A0FC51A25C94B005BA7BC /* Player.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Player.swift; path = ../Sources/Player.swift; sourceTree = "<group>"; };
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 = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -152,6 +154,7 @@
isa = PBXGroup;
children = (
065A0FC51A25C94B005BA7BC /* Player.swift */,
F87326A41EF057C7008D2915 /* FullScreenPlayer.swift */,
);
name = Player;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
);
Expand Down
18 changes: 18 additions & 0 deletions Project/Player/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ViewController: UIViewController {

fileprivate var player = Player()


// MARK: object lifecycle
deinit {
self.player.willMove(toParentViewController: self)
Expand Down Expand Up @@ -61,13 +62,24 @@ 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) {
super.viewDidAppear(animated)

self.player.playFromBeginning()
}

func tapOnFullscreen(){
player.enterFullScreen()
}
}

// MARK: - UIGestureRecognizer
Expand Down Expand Up @@ -104,6 +116,7 @@ extension ViewController:PlayerDelegate {
}

func playerPlaybackStateDidChange(_ player: Player) {
print(player.playbackState)
}

func playerBufferingStateDidChange(_ player: Player) {
Expand All @@ -130,5 +143,10 @@ extension ViewController:PlayerPlaybackDelegate {
func playerPlaybackWillLoop(_ player: Player) {
}

func playerEnterFullScreen() {
}

func playerExitFullScreen() {
}
}

16 changes: 15 additions & 1 deletion Sources/Player.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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
Expand Down