Trying to extract Song Array from Shazamkit

I am try to extract the audio file url from Shazamkit, it is deep inside the hierarchy of SHMediaItem > songs > previewAssets > url

when I access the url with like this:

let url = firstItem.songs[0].previewAssets?[0].url

I am getting a warning like this:

here is the Variable Viewer

this is what I have done so far:

struct MediaItems: Codable {
    let title: String?
    let subtitle: String?
    let shazamId: String?
    let appleMusicId: String?
    let appleMusicUrL: URL?
    let artworkUrl: URL?
    let artist: String?
    let matchOffset: TimeInterval?
    let videoUrl: URL?
    let webUrl: URL?
    let genres: [String]
    let isrc: String?
    let songs: [Song]?
}

extension SwiftFlutterShazamKitPlugin: SHSessionDelegate{
    public func session(_ session: SHSession, didFind match: SHMatch) {
        let mediaItems = match.mediaItems
        if let firstItem = mediaItems.first {

            // extracting the url
            let url = firstItem.songs[0].previewAssets?[0].url

            let _shazamMedia = MediaItems(
                title:firstItem.title!,
                subtitle:firstItem.subtitle!,
                shazamId:firstItem.shazamID!,
                appleMusicId:firstItem.appleMusicID!,
                appleMusicUrL:firstItem.appleMusicURL!,
                artworkUrl:firstItem.artworkURL!,
                artist:firstItem.artist!,
                matchOffset:firstItem.matchOffset,
                videoUrl:firstItem.videoURL!,
                webUrl:firstItem.webURL!,
                genres:firstItem.genres,
                isrc:firstItem.isrc!,
                songs:firstItem.songs
            )
            
            do {
                let jsonData = try JSONEncoder().encode([_shazamMedia])
                let jsonString = String(data: jsonData, encoding: .utf8)!
                self.callbackChannel?.invokeMethod("matchFound", arguments: jsonString)
            } catch {
                callbackChannel?.invokeMethod("didHasError", arguments: "Error when trying to format data, please try again")
            }
        }
    }
  • It looks like the warning you are getting might be because you're trying to encode the MusicKit Song using Codable.

    SHMediaItem actually conforms to NSSecureCoding in ObjC. Is there a reason why you need to declare your own MediaItems? You might be able to just use NSKeyedArchiver.archivedData to encode the SHMediaItem itself.

    firstItem.songs[0].previewAssets?[0].url should work, but I would recommend using first? instead of the subscript, so you can check for nil values properly.

  • I added my own MediaItem to resolve the "Failed decode" warning but that didn't work. Even if I use let url = firstItem.songs.first?.previewAssets?.first?.url I'm still getting the same warning.

    In thee Variable Viewer we see _rawResponseSongs Dictionary, I need to access that without triggering the warning.

Add a Comment

Accepted Reply

Not solved

Replies

Not solved