Skip to content

Instantly share code, notes, and snippets.

@meyusufdemirci
Created January 30, 2018 18:36
Show Gist options
  • Save meyusufdemirci/378cc7de3fa3f41c97f4d51660b665d8 to your computer and use it in GitHub Desktop.
Save meyusufdemirci/378cc7de3fa3f41c97f4d51660b665d8 to your computer and use it in GitHub Desktop.
import UIKit
import AVKit
class ViewController: UIViewController {
let playerController = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
// if you have a url of video
// let player = AVPlayer(url: URL(string: "video url")!)
let path = Bundle.main.path(forResource: "Mahmut Orhan - Game Of Thrones", ofType: "mp4")
// if you have a path of local video
let player = AVPlayer(url: URL(fileURLWithPath: path!))
playerController.player = player
playerController.view.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 250)
self.view.addSubview(playerController.view)
playerController.player?.play()
NotificationCenter.default.addObserver(self, selector: #selector(onRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
@objc func onRotated() {
// portrait
if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
playerController.view.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 250)
}
// landscape
else if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
playerController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment