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

时间:2018-09-16 14:12:31

标签: ios xcode swift4 carplay

我是CarPlay的新手,了解它与UITableViewController相似,但略有不同,并且无法使行能够被运行以运行函数/代码。网上没有太多信息,没有样品。苹果工程师告诉我,他们很快将在其开发人员站点上为音频应用提供示例。到目前为止,我正在运行一切,这就是我所缺少的。我有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){
    }

}

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用:

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

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