0

I have a video background for the welcome screen of the app but when I run only a blank screen appears.

The string for the bundle path for resource part is perfectly correct so a typo isn't the problem or anything. When I put a breakpoint on the "if path" block though it isn't getting called. This is in view did load and "AVKit" and "AVFoundation" are both imported and there are no errors.

Any insights? Thanks!

    let moviePath = NSBundle.mainBundle().pathForResource("Ilmatic_Wearables", ofType: "mp4")

    if let path = moviePath {

        let url = NSURL.fileURLWithPath(path)
        let player = AVPlayer(URL: url)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        playerViewController.view.frame = self.view.bounds
        self.view.addSubview(playerViewController.view)
        self.addChildViewController(playerViewController)
        player.play()
    }
Echizzle
  • 3,359
  • 5
  • 17
  • 28

2 Answers2

1

Weird, your code should work if moviePath isn't nil as you are saying. Check this way:

if let moviePath != nil {
       ...
}

Update

Check if your video file's Target Membership is set

nsinvocation
  • 7,559
  • 3
  • 41
  • 46
  • Yea.. I tried that but the thing is it is equal to nil so it still won't execute the code inside the block. I'm just not sure why it's nil if the path for resource is written correctly. – Echizzle Nov 12 '15 at 21:09
  • Have you checked video file's target membership? – nsinvocation Nov 12 '15 at 21:33
  • I have not, how do I do that? – Echizzle Nov 12 '15 at 22:33
  • First, select your video file, then in the right pane select 'File Inspector', you can find 'Target Membership' between 'Localisation' and 'Source Control' options. – nsinvocation Nov 13 '15 at 07:06
  • Nice it worked! I would like to make the video fill the frame though, do you by chance know how can I make it so the video itself fits the frame size? – Echizzle Nov 13 '15 at 21:46
  • Use AVPlayerLayer class. Btw don't forget to accept the answer it will help other people. – nsinvocation Nov 14 '15 at 07:25
0

EDIT (11/13/2015) - Try this:

Right Click, delete the video file from your project navigator, select move to trash.

Then drag the video from finder back into your project navigator.

When choosing options for adding these files:

  • check Copy items if needed
  • check add to targets

Rebuild your project, see if it works now!

I have included sample code that works for me:

import AVFoundation
import AVKit

class ExampleViewController: UIViewController {
    
    func loadAndPlayFile() {
        
        guard let path = NSBundle.mainBundle().pathForResource("filename", ofType: "mp4") else {
            print("Error: Could not locate video resource")
            return
        }
        
        let url = NSURL(fileURLWithPath: path)
        let moviePlayer = AVPlayer(URL: url)
        let moviePlayerController = AVPlayerViewController()
        moviePlayerController.player = moviePlayer
        
        presentViewController(moviePlayerController, animated: true) {
            moviePlayerController.player?.play()
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        loadAndPlayFile()
    }
}

/edit


Check to see if the video file plays in iTunes. If it plays in iTunes, it should also play on the iOS simulator and on a device.

If it doesn't play in iTunes, but does work with VLC Media Player, Quicktime, etc. It need to be re-encoded.

iOS supports many industry-standard video formats and compression standards, including the following:

H.264 video, up to 1.5 Mbps, 640 by 480 pixels, 30 frames per second, Low-Complexity version of the H.264 Baseline Profile with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats H.264 video, up to 768 Kbps, 320 by 240 pixels, 30 frames per second, Baseline Profile up to Level 1.3 with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats MPEG-4 video, up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats Numerous audio formats, including the ones listed in Audio Technologies

Community
  • 1
  • 1
Brandon Nott
  • 536
  • 2
  • 15
  • It does play in iTunes yet doesn't show up on the device. Very odd, you think there's something I need to put in the info.plist or anything of the sort? – Echizzle Nov 12 '15 at 21:49