我将分享在 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 权利的步骤