gpt4 book ai didi

ios - 如何在CarPlay上播放视频?

转载 作者:行者123 更新时间:2023-12-01 16:07:17 29 4
gpt4 key购买 nike

我有一个视频链接,在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)
}
}
}

最佳答案

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

关于ios - 如何在CarPlay上播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56938924/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com