0

I want to have audio integration to a carplay application. I have created a CPListTemplate with CPListItems and on tap of it, i am pushing a Now playing template. The audio does not seem to play and i cannot interact with the now playing screen.

Question:

  1. How do i pass the Audio URl to the now playing template so it can at-least play the sound?
  2. How do i interact with the player and display album artwork and text on the screen ?

Note: I am testing this on simulator and i have the Audio URL's in mp3 format.

     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)   
Shashant
  • 1
  • 1
  • Hi Shashant, can you post some code of what you have tried. Does your app play the audio and display the artwork on the device outside of CarPlay ? – Shawn Frank Aug 03 '21 at 14:31
  • Hi, I have added part of the implementation. The PlaylistItem object has the audio file as an mp3 and some other details like the title and album image. I am not sure we have to pass the audio url somewhere to the nowplaying object to initiate the playing of sound. – Shashant Aug 03 '21 at 18:39
  • Truptika's answer should work. Please note, the CarPlay Audio Templates are just responsible for the UI elements of your app. You need to handle the playing using AVPlayer or other audio libraries. – Shawn Frank Aug 04 '21 at 07:47
  • Yes shawn, that worked. thank you ! – Shashant Aug 04 '21 at 08:55

1 Answers1

0
  1. How do i pass the Audio URl to the now playing template so it can at-least play the sound?

There is no way to pass url in now playing template. Use AVPlayer to play audio url. Add setPlayer() method on item handler and pass your audio url in place of "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. How do i interact with the player and display album artwork and text on the screen ?

Display album artwork image and text in now playing template set media information in 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
}
Truptika
  • 41
  • 4
  • Thanks @truptika, this has helped. I was able to play the sound plus the album artwork is also getting set on the carplay screen. Though a bit weird behaviour on the simulator, for the first time the screen is not interactive but second gets activated the next time onwards. – Shashant Aug 04 '21 at 08:52
  • @Shashant - the CarPlay simulator is not highly reliable and has such issues. However, when you say not interactive and gets activated - what controls or UI elements are you speaking about ? – Shawn Frank Aug 06 '21 at 02:50
  • Well on the first push to the nowplaying template.. the audio starts playing in the background and the template is non-interactive and when i go back to the list and push the template again, the now playing template gets interactive and i can play/pause the audio. – Shashant Aug 10 '21 at 11:54