【问题标题】:How to play videos on CarPlay?如何在 CarPlay 上播放视频?
【发布时间】:2019-11-18 04:55:20
【问题描述】:

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

【问题讨论】:

  • 你找到解决办法了吗
  • @AJSanjay 不,抱歉 :(
  • 您好,我找到了在 carplay 上播放视频的解决方案。但是要在设备上测试它,我需要一个在苹果开发者计划中注册的苹果证书

标签: ios swift avfoundation avplayer carplay


【解决方案1】:

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

【讨论】:

  • 有没有办法不用注册苹果开发者计划就可以测试
  • 不可能的
  • @F.Sviatoslav 但是这个应用程序不会被苹果审查团队接受。
  • @AJSanjay 是的,我明白了,它仅供内部使用,无论如何谢谢:)
  • 不客气
猜你喜欢
  • 2017-07-31
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
相关资源
最近更新 更多