Instance Property

nowPlayingInfo

The now-playing information, such as title and artwork, for the media.

Declaration

var nowPlayingInfo: [String : Any]? { get set }

Discussion

In your confirm(intent:completion:) response, provide now-playing information for the media so Siri can display it to the user at the appropriate time, such as after they connect headphones to their iPhone.

To create the now-playing information dictionary, use the media item property keys from the Media Player framework (see General Media Item Property Keys for the list of keys). To include artwork in the dictionary, use INImage instead of UIImage to create the image object. Media intent shortcuts don't support UIImage objects in this dictionary.

Listing 1

Creating a now-playing information dictionary

public static func nowPlayingInfo(for episode: PodcastEpisode, in container: LibraryItemContainer, forShortcut: Bool = false) -> [String: Any] {
    var nowPlayingInfo: [String: Any] = [MPMediaItemPropertyTitle: episode.title,
                                         MPMediaItemPropertyMediaType: MPMediaType.podcast.rawValue,
                                         MPMediaItemPropertyPodcastTitle: container.title,
                                         MPMediaItemPropertyPlaybackDuration: NSNumber(value: audioFileDuration)]

    if forShortcut {
        nowPlayingInfo[MPMediaItemPropertyArtwork] = INImage(named: container.artworkName)
    } else {
        if let image = UIImage(named: container.artworkName) {
            let artwork = MPMediaItemArtwork(boundsSize: CGSize(width: 60, height: 60)) { (_) -> UIImage in
                return image
            }
            
            nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
        }
    }
    
    return nowPlayingInfo
}