Reputation: 53
I have a web link to a video, in iOS I can open it with AVFoundation/AVKit frameworks inside AVPlayerViewController/AVPlayer.
But how can I open it in CarPlay?
Text from CarPlay documentation:
You cannot play video media items directly using the Media Player framework. To play back videos containing MPMediaItem objects, use an AVPlayer object from AVFoundation. The system player also provides a way to play video items using the system apps.
extension AppDelegate: CPListTemplateDelegate {
func listTemplate(_ listTemplate: CPListTemplate, didSelect item: CPListItem, completionHandler: @escaping () -> Void) {
if let url = item.userInfo as? String {
self.playVideo(url)
}
}
}
Upvotes: 2
Views: 1956
Reputation: 1440
I will share the steps to follow to play a sample video on carplay
in Appdeligate.h Add the following header file
import CarPlay
import AVKit
Then add CPApplicationDelegate
Add these deligate methords
// 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!")
}
Also proceed the steps to add carplay entitlements
Upvotes: 2