I have a ViewController with an AVPlayer, but the video is autorotate even when I have set portrait as the only supported orientation in info.plist and in delegate supportedInterfaceOrientationsFor function.
I have search for documentation without succeed.
Here is some code:
var videoPlayer = AVPlayerViewController()
var player = AVPlayer.init(url: URL.init(fileURLWithPath: urlString))
videoPlayer?.player = player
videoPlayer?.view.frame = self.view.frame
videoPlayer?.showsPlaybackControls = false
self.present(videoPlayer!, animated: false, completion: {action in
self.player?.play()
})
This can be done by subclassing AVPlayerViewController and return UIInterfaceOrientationMask.portrait from supportedInterfaceOrientations here is the code.
class PlayerVideoViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
return .portrait
}}
Thank you to Rhythmic Fistman for the answer.
Related
I have already successfully hidden the playback like this controller.showsPlaybackControls = false, but this does not actually disable the controls. I can still drag around in my View to change the Video playback.
Is the an easy way to disable these controls?
I have this Custom Video Player:
struct CustomVideoPlayer: UIViewControllerRepresentable {
var player: AVPlayer
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = false
controller.videoGravity = .resizeAspectFill
player.actionAtItemEnd = .none
NotificationCenter.default.addObserver(context.coordinator, selector: #selector(context.coordinator.restartPlayback), name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
}
class Coordinator: NSObject {
var parent: CustomVideoPlayer
init(parent: CustomVideoPlayer) {
self.parent = parent
}
#objc func restartPlayback() {
parent.player.seek(to: .zero)
}
}
}
This might not be the right way to do it, but for now I got a working solution. I just added .allowsHitTesting(false) on my VideoPlayer.
I have a controller with a AVPlayer in collection view cell. When orientation changes to Landscape, the player should get FullScreen.
For this I am presenting an AVPlayerController with same instance of player in Collection View Cell. The video works fine when it is rotated in playing mode.
However, when video is paused and I change orientation to Landscape, the frame at current moment changes i.e video moves forward.
I have tested, even when the orientation is kept same, when player is passed, the duration skips few seconds.
Here is the code:
In ViewController where cell is present.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let videoCell = contentGalleryController.curatorVideoCell else {return}
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
let player = videoCell.getVideoPlayer
playerViewController = (storyboard!.instantiateViewController(withIdentifier: "FullScreenPlayerController") as! FullScreenPlayerController)
playerViewController?.player = player
playerViewController?.didPlayedToEnd = videoCell.isVideoFinishedPlaying ?? false
playerViewController?.isMuteTurnedOn = player.isMuted
let wasVideoPlaying: Bool = player.isPlaying
present(playerViewController!, animated: false){
if wasVideoPlaying {
player.play()
}
}
}
else {
videoCell._setMuteIcon()
videoCell._setPlayPauseIcon()
playerViewController?.dismiss(animated: false, completion: nil)
}
}
In FullscreenPlayer View Controller
override func viewDidLoad() {
super.viewDidLoad()
setPlayPauseIcon()
setMuteIcon()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customControlViewTapped))
customView.addGestureRecognizer(tapGesture)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !view.subviews.contains(customView) {
customView.frame = view.bounds
view.addSubview(customView)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidPlayToEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
I am doing nothing else in controller.
Screenshots:
Portrait
LANDSCAPE
When orientation changes, video moves forward even on pause state.
Thanks for help in advance.
You should try capturing the currentTime and then use the seek(to time: CMTime) method on the player to start at that exact time. I am not sure exactly why this is happening to you, but I think this would get you the result you are looking for.
Okay So I have one view controller that is an AVPlayer. I want that controller to be able to rotate to landscape mode and portrait freely
import Foundation
import UIKit
import AVFoundation
import AVKit
class EventPromoVideoPlayer: UIViewController {
public var eventKey = ""
override var prefersStatusBarHidden: Bool {
return true
}
//URL of promo video that is about to be played
private var videoURL: URL
// Allows you to play the actual mp4 or video
var player: AVPlayer?
// Allows you to display the video content of a AVPlayer
var playerController : AVPlayerViewController?
init(videoURL: URL) {
self.videoURL = videoURL
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.gray
let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(_:)))
downSwipe.direction = .down
view.addGestureRecognizer(downSwipe)
//Setting the video url of the AVPlayer
player = AVPlayer(url: videoURL)
playerController = AVPlayerViewController()
guard player != nil && playerController != nil else {
return
}
playerController!.showsPlaybackControls = false
// Setting AVPlayer to the player property of AVPlayerViewController
playerController!.player = player!
self.addChildViewController(playerController!)
self.view.addSubview(playerController!.view)
playerController!.view.frame = view.frame
// Added an observer for when the video stops playing so it can be on a continuous loop
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player!.currentItem)
//TODO: Need to fix frame of x and y
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
player?.play()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isHidden = true
tabBarController?.tabBar.isHidden = true
}
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all
}
// Allows the video to keep playing on a loop
#objc fileprivate func playerItemDidReachEnd(_ notification: Notification) {
if self.player != nil {
self.player!.seek(to: kCMTimeZero)
self.player!.play()
}
}
#objc func cancel() {
dismiss(animated: true, completion: nil)
}
#objc func swipeAction(_ swipe: UIGestureRecognizer){
if let swipeGesture = swipe as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
break
case UISwipeGestureRecognizerDirection.down:
print("Swiped Down")
dismiss(animated: true, completion: nil)
break
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
break
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
break
default:
break
}
}
}
}
When I dismiss the screen I still need the previous controller to be in portrait. This shouldnt be a problem seeing as portrait mode is locked in as the only orientation in my settings and I want to keep it that way. However I want this one screen to be able to move freely. Any ideas and hints would be greatly appreciated.
The app delegate method does not work for me.
Neither does overriding shouldAutoRotate or supportedInterfaceOrientations in the function
You can use the below method in app delegate.
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = UIApplication.topViewController() {
if (rootViewController.responds(to: Selector(("canRotate")))) || String(describing: type(of: rootViewController)) == "AVFullScreenViewController" {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown;
}
}
// Only allow portrait (standard behaviour)
return .portrait
}
Add the below method in view controller where you want to display as landscape.
func canRotate() -> Void {}
extension UIApplication {
class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
Im currently using 'AVKit' and 'AVFoundation' to enable the video playing through my app but I can only done by starting the video by tapping a button. But I am currently facing a problem of trying to making an automatic function like playing the video footage right after we start an app on an iOS device.
override func viewDidLoad()
{
super.viewDidLoad()
self.playingVideo()
self.nowPlaying(self)
}
#IBAction func nowPlaying(_ sender: Any)
{
self.present(self.playerController, animated: true, completion:{
self.playerController.player?.play()
})
after I compiled it, the system printed out:
Warning: Attempt to present on whose view is not in the window hierarchy!
Please try following working code.
import AVFoundation
import UIKit
class SomeViewController: UIViewController {
func openVideo() {
let playerController = SomeMediaPlayer()
playerController.audioURL = URL(string: "Some audio/video url string")
self.present(playerController, animated: true, completion: nil)
}
}
class SomeMediaPlayer: UIViewController {
public var audioURL:URL!
private var player = AVPlayer()
private var playerLayer: AVPlayerLayer!
override func viewDidLoad() {
super.viewDidLoad()
self.playerLayer = AVPlayerLayer(player: self.player)
self.view.layer.insertSublayer(self.playerLayer, at: 0)
let playerItem = AVPlayerItem(url: self.audioURL)
self.player.replaceCurrentItem(with: playerItem)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.player.play()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.playerLayer.frame = self.view.bounds
}
// Force the view into landscape mode if you need to
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .landscape
}
}
}
my app is a portrait only app, but I have one view controller which is displaying a live stream, using an AVPlayerViewController.
To allow landscape for the full screen view of that player I wrote this method in the AppDelegate.swift:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
var orientation = UIInterfaceOrientationMask.Portrait
if let presentedController = window?.rootViewController?.presentedViewController? {
if presentedController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
orientation = .AllButUpsideDown
} else if let navController = presentedController as? UINavigationController {
if navController.topViewController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
orientation = .AllButUpsideDown
}
}
}
return Int(orientation.rawValue)
}
This is how I call initialise my AVPlayer:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLiveStream" {
SVProgressHUD.show()
var queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
let streamURL = NSURL(string: liveStreamURL)
let playerItem = AVPlayerItem(URL: streamURL)
let player = AVPlayer(playerItem: playerItem)
dispatch_async(dispatch_get_main_queue(), {
SVProgressHUD.dismiss()
var playerViewController = segue.destinationViewController as AVPlayerViewController
playerViewController.player = player
})
})
}
}
The problem: when I open up the full screen view of the player, then change to landscape and then click "Done" to dismiss the full screen view, my app stays in landscape. But I want it to rotate to portrait again. How can I do that?
Rather than implementing application(_:supportedInterfaceOrientationsForWindow:) try implementing supportedInterfaceOrientations() on each view controller. So for example:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
This will ensure that the view controller cannot be displayed in landscape, so when dismissing the video player, it will come straight back to a portrait window.
Update Objective-C:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
What i did to fix this issue, create a new class with a 'viewDidLayoutSubviews' function and override it!
final class NewMoviePlayerViewController: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.bounds == contentOverlayView?.bounds {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
}
}
Add These Lines in Appdelegate and your Required View Controller
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
//LandScapeMode:- UIInterfaceOrientationMaskLandscape;
//PortraitMode:-
return UIInterfaceOrientationMaskPortrait
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//LandScapeMode:- UIInterfaceOrientationLandscapeRight;
// ProtraitMode:-
return UIInterfaceOrientationPortrait
}