我是CarPlay的新手,我知道它与UITableViewController相似,但略有不同,不能使行能够被点击来运行函数/代码。网上关于这个的信息不多,也没有样本。苹果工程师告诉我,他们将很快在他们的开发者网站上提供音频应用程序的样本。到目前为止,我所有的东西都在运行,这就是我所缺少的。我有来自苹果的权利,在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){
}
}发布于 2018-12-25 04:57:50
你有没有尝试过使用:
- (void)playableContentManager:(MPPlayableContentManager *)contentManager initiatePlaybackOfContentItemAtIndexPath:(NSIndexPath *)indexPath completionHandler:(void (^)(NSError * _Nullable))completionHandler;它是MPPlayableContentDelegate的一部分。从那里,我只需从我的应用程序中调用常规的play方法。希望这能有所帮助。
https://stackoverflow.com/questions/52354962
复制相似问题