2

我有一个视频的网络链接,在 iOS 中,我可以使用 AVPlayerViewController/AVPlayer 中的 AVFoundation/AVKit 框架打开它。

但是如何在 CarPlay 中打开它?

CarPlay 文档中的文本:

您不能使用媒体播放器框架直接播放视频媒体项目。要播放包含 MPMediaItem 对象的视频,请使用来自 AVFoundation 的 AVPlayer 对象。系统播放器还提供了一种使用系统应用程序播放视频项目的方法。

extension AppDelegate: CPListTemplateDelegate {
    func listTemplate(_ listTemplate: CPListTemplate, didSelect item: CPListItem, completionHandler: @escaping () -> Void) {
        if let url = item.userInfo as? String {
            self.playVideo(url)
        }
    }
}
4

1 回答 1

2

我将分享在 carplay 上播放示例视频的步骤

在 Appdeligate.h 添加如下头文件

import CarPlay
import AVKit

然后加CPApplicationDelegate

添加这些代理方法

 // MARK: - CPApplicationDelegate methods
func application(_ application: UIApplication, didConnectCarInterfaceController interfaceController: CPInterfaceController, to window: CPWindow) {
    print("[CARPLAY] CONNECTED TO CARPLAY!")

    // Keep references to the CPInterfaceController (handles your templates) and the CPMapContentWindow (to draw/load your own ViewController's with a navigation map onto)
    self.interfaceController = interfaceController
    self.carWindow = window
    guard let path = Bundle.main.path(forResource: "video", ofType: "mp4") else
                        {
                            return
                        }
                        let videoURL = NSURL(fileURLWithPath: path)
                        let player = AVPlayer(url: videoURL as URL)
                        let playerController = AVPlayerViewController()
                        playerController.player = player
                        player.play()

            window.rootViewController = playerController
}

func application(_ application: UIApplication, didDisconnectCarInterfaceController interfaceController: CPInterfaceController, from window: CPWindow) {
    print("[CARPLAY] DISCONNECTED FROM CARPLAY!")
}

还继续执行添加 carplay 权利的步骤

于 2020-02-24T06:42:59.150 回答