Search code examples
swifttvos

Can't play local video file


I'm trying to play a local video using Xcode 7.1. All I want is for the video to load full screen on view and play.

import UIKit
import AVFoundation
import AVKit

class myClass: AVPlayerViewController {
    private func playVideo() {
        if let path = NSBundle.mainBundle().pathForResource(“video”, ofType: "mp4") {
            let url = NSURL(fileURLWithPath: path)
            player = AVPlayer(URL: url)
            player?.play()
        }
        else {
            print("Oops, something wrong when playing video.mp4")
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playVideo() 
    } 
}

Solution

  • I managed to find a fix.

    import Foundation
    import UIKit
    import AVKit
    
    class PlayerViewController: AVPlayerViewController, AVPlayerViewControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        func playVideo() {
    
            if let path = NSBundle.mainBundle().pathForResource("video", ofType: "mp4") {
                let url = NSURL(fileURLWithPath: path)
                player = AVPlayer(URL: url)
                player?.play()
            }
            else {
                print("Oops, something wrong when playing video.mp4")
            }
        }
    
        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)
            playVideo()
        }
    }