ios - 如何使 CarPlay 中音频应用程序的行可点击?

标签 ios xcode swift4 carplay

我是 CarPlay 的新手,我知道它类似于 UITableViewController 但略有不同,并且无法使行能够被点击以运行函数/代码。网上关于此的信息不多,没有样本。 Apple 工程师告诉我他们将很快在他们的开发者网站上提供音频应用程序的示例。到目前为止,我的一切都在运行,这就是我所缺少的。我有 Apple 的权利,在 plist 中添加了所需的内容。在实际 CarPlay 设备中一切都显示正常。我有两个带有示例项目的选项卡,一个我需要在点击时运行流媒体实时媒体的功能,另一个到播客列表,需要加载列表并使其可点击以运行功能。

这是我到目前为止一直在测试的内容:

protocol MPPlayableContentDelegate {
    func dataReceived(data: String)
}


//Check data:       indexPath.count == 2 && indexPath[0] == 0 && indexPath[1] == 0
class CarplayDataSource: NSObject, MPPlayableContentDataSource, MPPlayableContentDelegate {

    let dataStore = NSObject()
    var dataHelper: DataHelper!
    var podcastData = [PodcastObject]()

    var podcastItem = PlayerViewController()

    var delegate: MPPlayableContentDelegate! = nil

    func dataReceived(data: String) {
        delegate?.dataReceived(data:"data received")
    }

    override init() {
        super.init()
        print("CARPLAY: init")
        self.dataReceived(data: "data is now received")
    }

    func numberOfChildItems(at indexPath: IndexPath) -> Int {
        print("CARPLAY: number of items")

        if indexPath.indices.count == 0 {
            print("CARPLAY: number of items > indices.count == 0 > return 2")
            return 2
        } else if indexPath.indices.count == 1 {
            print("CARPLAY: number of items > indices.count == 1 > return 1")
            return 1
        }
        return 1
    }


    func contentItem(at indexPath: IndexPath) -> MPContentItem? {
        print("CARPLAY: contentItem at indexpath")

        let indexCount = indexPath.count
        let tabIndex = indexPath[0]
        if indexCount == 1 {

            if tabIndex == 0 {
                let item = MPContentItem(identifier: "Tab \(tabIndex)")

                item.title = "Radio"
                item.isContainer = true
                item.isPlayable = false
                item.artwork = MPMediaItemArtwork(image: #imageLiteral(resourceName: "radio-carplay"))
                return item
            } else {

                let item = MPContentItem(identifier: "Tab \(tabIndex)")
                item.title = "Podcasts"
                item.isContainer = true
                item.isPlayable = false
                item.artwork = MPMediaItemArtwork(image: #imageLiteral(resourceName: "recordings-carplay"))
                return item
            }
        } else if indexCount == 2 {

            if tabIndex == 0 {
                let item = MPContentItem(identifier: "Tab \(tabIndex) Item")
                item.title = "Listen Live" //= "Tab \(tabIndex) Item"
                item.subtitle = "Radio Subtitle!"
                item.isPlayable = true
                item.artwork = MPMediaItemArtwork(image: #imageLiteral(resourceName: "menulogo"))

                if #available(iOS 10.0, *) {
                    item.isStreamingContent = true
                }
                return item
            } else {
                let item = MPContentItem(identifier: "Tab \(tabIndex) Item")
                item.title = "Coming Soon!" //for now since I don't now how to bring the list in
                item.isPlayable = true
                return item
            }

        }
        return nil

    }

    func contentItem(forIdentifier identifier: String, completionHandler: @escaping (MPContentItem?, Error?) -> Swift.Void){
    }

}

最佳答案

您是否尝试过使用:

- (void)playableContentManager:(MPPlayableContentManager *)contentManager initiatePlaybackOfContentItemAtIndexPath:(NSIndexPath *)indexPath completionHandler:(void (^)(NSError * _Nullable))completionHandler;

它是 MPPlayableContentDelegate 的一部分。从那里我只是从我的应用程序调用常规播放方法。希望这会有所帮助。

关于ios - 如何使 CarPlay 中音频应用程序的行可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52354962/

相关文章:

iphone - iOS - UiTableviewCell - 背景图片一次只显示在一个单元格上

ios - NSFetchedResultsController 和使用关系的表

iOS模拟器屏幕尺寸

SwiftUI:NavigationLink 在列表中时始终处于激活状态

ios - Realm Swift - 对象中可变数量的值

ios - Facebook 作者 user.credential 返回 nil

ios - "Application windows are expected..."警告,但应用运行正常

ios - Xcode 9 中的自适应表格 View 单元格

ios NavigationBarHidden 在 vi​​ewDidLoad 中不起作用

ios - 如何在使用 Realm 的同时实现 Codable