D.Bo D.Bo - 1 year ago 342
iOS Question

AVPlayer Swift: How do I hide controls and disable landscape view?

since this is my first post, just a few words about me: Usually I design stuff (UI primarily) but I really wanted to take a leap into programming to understand you guys better. So I decided build a small app to get going.

So I've been trying to figure this out for hours now – this is my first app project ever so I apologise for my newbyness.

All I want to do is to hide the controls of AVPlayer and disable landscape view but I just can't figure out where to put

showsPlaybackControls = false

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

private var firstAppear = true

override func viewDidAppear(animated: Bool) {
if firstAppear {
do {
try playVideo()
firstAppear = false
} catch AppError.InvalidResource(let name, let type) {
debugPrint("Could not find resource \(name).\(type)")
} catch {
debugPrint("Generic error")


private func playVideo() throws {
guard let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4") else {
throw AppError.InvalidResource("video", "m4v")
let player = AVPlayer(URL: NSURL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: false) {

enum AppError : ErrorType {
case InvalidResource(String, String)

Answer Source

showsPlaybackControls is a property of AVPlayerViewController, so you can set it after you create it:

playerController.showsPlaybackControls = false

If you want to allow only landscape, you can subclass AVPlayerViewController, override supportedInterfaceOrientations and return only landscape, then use that class instead of AVPlayerViewController.