EdDry
EdDry

Reputation: 141

Cannot instantiate AVPlayerViewController error

Experimenting with creating a simple app to play video on the iPhone (Xcode 7.3, swift iOS9) and I'm just trying to get the player to appear before adding code to load content.

Using storyboard, created a button and segue (show) to an AVViewController object. When app is run on device and button is pressed, I get:

"Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named AVPlayerViewController' "

Prior to declaring my ViewContrller: UIViewController class I have: import UIKit, import AVKit and import AVFoundation. What's missing? I suspect something else I need to import but my research has only turned up AVKit and AVFoundation as necessary.

Upvotes: 3

Views: 1106

Answers (2)

As @EdDry mentioned above. If you want to use AVPlayerViewController from storyboard, then follow the steps:

  1. You must link the AVKit framework explicitly to your target.

enter image description here

  1. Add a simple ViewController with a button. Then add the AVKit Player View Controller and finally a triguered segue to show the player controller.

enter image description here

  1. Create a custom class for the ViewController (for example ButtonViewController in my case). Use this url https://download.samplelib.com/mp4/sample-5s.mp4 to load the video.

enter image description here

  1. Don't forget to set the custom class to your ViewController.

enter image description here

  1. Run the app

enter image description here

Upvotes: 0

EdDry
EdDry

Reputation: 141

I found a solution while viewing answers to other similar problems here on Stack Overflow.

To allow the app to run correctly I had to manually link the frameworks to my project.

In Xcode I clicked on my project's name to get to settings, then added AVFoundation.framework and AVKit.framework in the Linked Frameworks and Libraries section. Now running the app allowed me to instantiate a AVPlayerViewController object without any problems. Apparently, importing AVKit and AVFoundation in my code was not enough. Anyone know why I had to explicitly link the frameworks?

Upvotes: 8

Related Questions