Search code examples
xcodeswiftvideoapple-tv

AppleTV + Swift + MP4 + Xcode? How to show a .mp4 inside xcodeproj?


how would I go about using Swift & AppleTV4 to display a video that is inside the XcodeProj? Can I use the AVPlayer to also play an Mp3 later?

This is what I have:

 import UIKit
 import AVFoundation
 import AVKit
 import MediaPlayer

 class ViewController: UIViewController {
let videoplayer = AVPlayer(URL: NSURL(string: "file.mp4")!)
let audioplayer = AVPlayer(URL: NSURL(string: "file.mp3")!)


@IBOutlet weak var Buttonizer: UIButton!

@IBAction func ButtonPressed(sender: AnyObject) {

    videoplayer.play()
    audioplayer.play()

  // let videoplayer = AVPlayer(URL: NSURL(string: "https://clips.vorwaerts-gmbh.de/VfE_html5.mp4")!)        
}
and the rest.. (viewDidLoad stuff)

I tried to run AVPlayer with an external mp4, but the videoplayer.play() simply crashes with a breakcode.


Solution

  • Try this to play a video in AVPlayer:

        let path = NSBundle.mainBundle().pathForResource("test", ofType:"mp4")!
        let videoURL = NSURL(fileURLWithPath: path)
        let player = AVPlayer(URL: videoURL)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        player.play()