作者:xo麦先生 | 来源:互联网 | 2023-06-13 16:00
carplay音频应用程序一直运行到iOS v12.4。当我创建下面的类时,我遵循了这个link的答案,并且效果很好。下面是示例代码。
正在播放的影片正在carplay上运行,但是在仪表板上打开该应用程序后,它显示无法连接。我正在使用Xcode v11.2.1(11B500),模拟器iOS v13.2.2,iPhone 11(v13.2.3)。
尝试在initiatePlaybackOfContentItemAt
上打印打印日志,但没有被调用。您认为我缺少什么,以便它可以正常运行并与iOS 13或更高版本兼容?
class PlayManager: NSObject {
static let shared = PlayManager()
var currentStation: Int64 = 0
var isRadio: Bool = true
var contentList: [RadioObj] = []
let commandCenter = MPRemoteCommandCenter.shared()
var nowPlayingSongManager: MPNowPlayingInfoCenter?
var playableContentManager: MPPlayableContentManager?
override init() {
super.init()
let artist = "Play"
nowPlayingSOngManager= MPNowPlayingInfoCenter.default()
nowPlayingSongManager?.nowPlayingInfo = [
MPNowPlayingInfoPropertyMediaType : "Audio",MPMediaItemPropertyTitle : api.defaults.defaultArtist,MPMediaItemPropertyArtist: artist
]
player.initializeMedia(song: api.defaults.defaultArtist,artist: artist,album: api.defaults.appLogo)
playableCOntentManager= MPPlayableContentManager.shared()
playableContentManager?.dataSource = self
playableContentManager?.delegate = self
}
func loadData(){
api.data.getRadioData(event_source: "carplay") { (done,obj) in
if done {
if obj.count > 0 {
let indeces: Int = 0
api.currentRadio = obj[indeces]
api.isRadio = true
api.ispodcast = false
}
self.cOntentList= obj
self.playableContentManager.reloadData()
}
}
}
}
extension PlayManager: mpplayablecontentdelegate,MPPlayableContentDataSource {
func playableContentManager(_ contentManager: MPPlayableContentManager,initiatePlaybackOfContentItemAt indexPath: IndexPath,completionHandler: @escaping (Error?) -> Void) {
print("check if its called")
let indeces: Int = indexPath[0]
let radio_name = contentList[indeces].name
let id = contentList[indeces].id
let radio_logo = contentList[indeces].logo_rounded_url
let stream_url = contentList[indeces].stream_url
let hd_stream_url = contentList[indeces].hd_stream_url
var currentIdentifier = ""
if contentManager.nowPlayingIdentifiers.count > 0 {
currentIdentifier = contentManager.nowPlayingIdentifiers[0]
}
var newIdenditier = "radio_"
if let id = id {
newIdenditier += "\(id)"
}
let radioObj = RadioObj()
radioObj.stream_url = stream_url
radioObj.hd_stream_url = hd_stream_url
radioObj.name = radio_name
radioObj.logo_url = radio_logo
api.currentRadio = contentList[indeces]
api.isRadio = true
if let id = id {
currentStation = id
}
guard let logo = radio_logo,let name = radio_name else { return }
player.initializeMedia(song: name,artist: api.defaults.defaultArtist,album: api.defaults.appLogo)
api.download(image: logo) { (image) in
player.initializeMedia(song: name,album: image)
}
if api.isPlaying && currentIdentifier != newIdenditier {
contentManager.nowPlayingIdentifiers = [newIdenditier]
DispatchQueue.main.async {
player.start()
}
}
onTapSound(contentManager: contentManager,completionHandler: completionHandler,indexPath: indexPath)
}
func onTapSound(contentManager:MPPlayableContentManager,completionHandler: @escaping (Error?) -> Void,indexPath : IndexPath){
completionHandler(nil)
}
func numberOfChildItems(at indexPath: IndexPath) -> Int {
if indexPath.count == 0 {
return contentList.count
} else if indexPath.count == 1 {
let _: Int = indexPath.first ?? 0
} else if indexPath.count == 2 {
}
return 0
}
func contentItem(at indexPath: IndexPath) -> MPContentItem? {
let indeces: Int = indexPath[0]
let _: Int = indexPath.indices.count
let radio_name = contentList[indeces].name
let id = contentList[indeces].id
let radio_logo = contentList[indeces].car_logo
if let id = id,let radio_logo = radio_logo {
let cOntentItem= MPContentItem(identifier: "radio_\(id)")
contentItem.title = radio_name
contentItem.isCOntainer= false
contentItem.isPlayable = true
contentItem.isStreamingCOntent= true
DispatchQueue.global().async {
if let url = URL(string:radio_logo) {
do {
let radio = try UIImage(data: Data(contentsof: url))
if let image = radio {
let artWork = MPMediaItemArtwork(boundsSize: image.size,requestHandler: { (size) -> UIImage in
return image
})
contentItem.artwork = artWork
}
} catch{}
}
}
return contentItem
}
return MPContentItem()
}
open func setupEvents(isRadio isTrue: Bool?) -> PlayManager {
guard let isTrue = isTrue else { return self }
commandCenter.playCommand.isEnabled = isTrue
if isTrue {
commandCenter.skipForwardCommand.isEnabled = false
commandCenter.skipBackwardCommand.isEnabled = false
commandCenter.stopCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = false
if #available(iOS 10.0,*) {
nowPlayingSongManager?.nowPlayingInfo?[MPNowPlayingInfoPropertyIsLiveStream] = true
}
} else {
commandCenter.stopCommand.isEnabled = false
commandCenter.pauseCommand.isEnabled = true
}
commandCenter.stopCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
api.isPlaying = false
player.stop()
return .success
}
commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
player.pause()
return .success
}
commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
api.isPlaying = true
if api.isRadio {
self.playableContentManager?.nowPlayingIdentifiers = ["radio_\(self.currentStation)"]
guard let logo = api.currentRadio?.logo_rounded_url,let name = api.currentRadio?.name else { return .noSuchContent }
api.download(image: logo) { (image) in
player.initializeMedia(song: name,album: image)
}
player.start()
}
if api.ispodcast {
player.resume()
}
return .success
}
return self
}
}
前一段时间,我向Apple提交了一个错误报告。苹果回到我身边说:
“我们需要进行系统诊断,理想情况下,一些示例代码可以重现
请发行。
beginLoadingChildItems()只是应用启动的提示
如果用户点击一行,则预取内容。代替,
客户端应用似乎没有提供任何内容的可能性更高
到MPPlayableContentManager。”
您可以看到此问题以前是在Apple Developer forums.
上提出的
CarPlay模拟器与Xcode 11和iOS 12.4模拟器配合良好,但Xcode 11和iOS 13模拟器似乎存在问题。我尝试在Xcode 11上构建我们的应用程序,并在具有物理车载CarPlay多媒体系统的物理iOS 13.1设备上运行它,它运行完美!这似乎只是一个CarPlay模拟器问题。
,
似乎是Xcode 11错误。我已经尝试过Xcode 13 beta,但是结果是一样的。
在我的情况下,有一个空白的白色屏幕,带有带有蓝色的空白标签。然后显示错误,提示“无法加载内容(空)” iOS 13 Simulator screenshot。
最终安装了iPhone 5s / iOS 12.4.1模拟器。
在带有真实设备的客户端方面,iOS 13正常工作。