0

I'm uploading the current playback information from the iOS Media Player to Firebase and want to be able to include the current artwork however I can't just put it as an MPMediaItemArtwork (as seen below) as the struct does not conform to the Codable format. How can I convert it into something that is e.g. a String?

import Firebase
import CodableFirebase
import MediaPlayer

    struct LatestPlaybackItem: Codable { 
        var trackName: String
        var trackArtistName: String
        var playlistName: String
        var playbackState: String
        var currentArtwork: MPMediaItemArtwork  
    }

I've looked at these answers but not sure how to get it working with an MPMediaItemArtwork.

My structure does not conform to protocol 'Decodable' / 'Encodable'

Codable class does not conform to protocol Decodable

Jack Vanderpump
  • 216
  • 2
  • 16
  • Basically you can’t. – matt Feb 05 '20 at 13:37
  • You'll need to define your custom encoding/decoding for `MPMediaItemArtwork` by implementing `init(from:)` and `encode(to:)` yourself. However, I'd rather suggest storing `currentArtwork` as a more suitable data type that is easier to encode/decode. – Dávid Pásztor Feb 05 '20 at 13:43
  • Surely not! Can I not code it up either as a string or Data format and then decode it on the other side (I want to make use of this information in a second app) – Jack Vanderpump Feb 05 '20 at 13:43
  • Thanks David, I guess I have to basically follow what is laid out here, right? https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types – Jack Vanderpump Feb 05 '20 at 13:47
  • Yes, that is correct – Dávid Pásztor Feb 05 '20 at 14:14
  • I haven't been able to figure it out myself. I would really appreciate it if you could elaborate further on what I need to! I guess I need to add an extension to MPMediaItemArtwork and add the `init(from:)` and `encode(to:)` there. Do I also need to create a CodingKey? – Jack Vanderpump Feb 05 '20 at 15:40

0 Answers0