Fetch music file from Cloudkit

485 Views Asked by At

I try to convert the code which i use to fetch image asset to fetch mp3 file on cloudkit. However, i can't figure the data part. I'm using this library to play audio. It only has one class called "AudioPlayer" so if i want to play music on local folder, it is enough to declare it.

https://github.com/tbaranes/AudioPlayerSwift

   func loadSong(completion:@escaping (_ song: AudioPlayer?) -> ()) {
            // 1
            DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
                var song: AudioPlayer!
                defer {
                    completion(song)
                }
                // 2
                guard let asset = self.record["Song_File"] as? CKAsset else {
                    return
                }

                let songData: AudioPlayer
                do {
                    songData = try Data(contentsOf: asset.fileURL)
                } catch {
                    return
                }
                song = AudioPlayer(contentsOf: <#T##URL#>)

                      }
        }
1

There are 1 best solutions below

0
On

Actually, i can't do clearly what i want. (I wanted to stream a song from CloudKit, this one downloads the ckasset). Below code gets the URL of CkAsset so that i can put it in AVPlayer and play it.

func loadSongURL(completion:@escaping (_ url: URL?) -> ()) {
        // 1
        DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
            var song_url: URL!
            defer {
                completion(song_url)
            }
            // 2
            guard let asset = self.record["Song_File"] as? CKAsset else {
                return
            }

            let songURL: URL
            do {
                print(asset.fileURL)
                songURL = asset.fileURL
            } catch {
                return
            }
           song_url = songURL