swift 如何在CarPlay Audio App中通过实时URL集成音频

gj3fmq9x  于 2023-09-30  发布在  Swift
关注(0)|答案(1)|浏览(86)

我想有音频集成到一个carplay应用程序。我已经创建了一个CPListTemplate与CPListItems和它的水龙头,我推一个现在播放模板。音频似乎不能播放,我不能与现在播放的屏幕交互。
问:
1.如何将音频URL传递给现在播放的模板,以便它至少可以播放声音?
1.如何与播放器交互并在屏幕上显示专辑插图和文本?
注:我正在测试模拟器,我有音频URL的mp3格式。

var listItems: [CPListItem] = []
     for playListItem in PlayListItems {
          let item = CPListItem(text: playListItem.title, detailText: "")
          item.handler = {listItem, completion in 
          let nowPlayingTemplate = CPNowPlayingTemplate.shared
          nowPlayingTemplate.add(self)
          nowPlayingTemplate.isAlbumArtistButtonEnabled = true
          self.interfaceController?.pushTemplate(nowPlayingTemplate, animated: true)
          }
      listItems.append(item)
     }
     let sectionPlayList = CPListSection(items: listItems)
     self.interfaceController?.pushTemplate(CPListTemplate(title: “Tab 1”, sections: [sectionPlayList]), animated: true)
enyaitl3

enyaitl31#

1.如何将音频URL传递给现在播放的模板,以便它至少可以播放声音?
没有办法在现在播放的模板中传递URL。使用AVPlayer播放音频URL。在项目处理程序上添加setPlayer()方法,并传递音频URL代替“stationUrl”。

var player = AVPlayer()
func setPlayer() {
      let playerItem = AVPlayerItem(url: URL(string: stationUrl)!)
      player = AVPlayer(playerItem: playerItem)
      player.rate = 1.0
      player.play()
      self.setPlayerNowPlayingInformation()
  }

1.如何与播放器交互并在屏幕上显示专辑插图和文本?
在MPNowPlayingInfoCenter中显示正在播放的模板集媒体信息中的专辑插图图像和文本。

func setPlayerNowPlayingInformation() {
      var nowPlayingInfo: [String: Any] = [:]
                
      nowPlayingInfo = [MPMediaItemPropertyTitle: "Media Title"]
                
      if let image = UIImage (named: "carplay_app_icon") {
         nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size, requestHandler: { size -> UIImage in
             return image
          })
      }          
      MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

相关问题