无法实例化AVPlayerViewController错误

问题描述 投票:0回答:2

尝试创建一个简单的应用程序来在 iPhone 上播放视频(Xcode 7.3,swift iOS9),我只是想让播放器在添加代码加载内容之前出现。

使用故事板创建一个按钮并连接(显示)到 AVViewController 对象。当应用程序在设备上运行并按下按钮时,我得到:

“由于未捕获的异常‘NSInvalidUnarchiveOperationException’而终止应用程序,原因:‘无法实例化名为 AVPlayerViewController 的类’”

在声明我的 ViewContrller: UIViewController 类之前,我有: import UIKitimport AVKitimport AVFoundation。少了什么东西?我怀疑我还需要导入其他东西,但我的研究只在必要时发现了 AVKit 和 AVFoundation。

swift ios9 avplayerviewcontroller
2个回答
8
投票

我在 Stack Overflow 上查看其他类似问题的答案时找到了解决方案。

为了让应用程序正确运行,我必须手动将框架链接到我的项目。

在 Xcode 中,我单击项目名称进行设置,然后在“链接的框架和库”部分添加 AVFoundation.framework 和 AVKit.framework。现在运行应用程序允许我实例化 AVPlayerViewController 对象,没有任何问题。显然,在我的代码中导入 AVKit 和 AVFoundation 是不够的。有人知道为什么我必须显式链接框架吗?


0
投票

正如上面@EdDry提到的。如果您想使用故事板中的

AVPlayerViewController
,请按照以下步骤操作:

  1. 您必须将
    AVKit framework
    明确链接到您的目标。

  1. 添加一个带有按钮的简单
    ViewController
    。然后添加
    AVKit Player View Controller
    ,最后添加
    triguered segue
    以显示玩家控制器。

  1. 为 ViewController 创建一个自定义类(例如我的例子中的
    ButtonViewController
    )。使用此网址 https://download.samplelib.com/mp4/sample-5s.mp4 加载视频。

  1. 不要忘记将自定义类设置为您的
    ViewController

  1. 运行应用程序

© www.soinside.com 2019 - 2024. All rights reserved.