I have a song stored in CloudKit as a CKAsset. I successfully retrieve the song and get no error but it just won't play. Any help would be appreciated, thanks.
My AVPlayer is lazily instantiated within the class.
//Create song player to play song, lazily instantiated
lazy var songPlayer: AVPlayer = {
let player = AVPlayer()
return player
}()
func playPause() {
//Unwrap song - if there is no song, then return...you can't play something that is not there.
guard let song = selectedArtist.song else {
return
}
//Create player item
let playerItem = AVPlayerItem(url: song.fileURL)
//Add observer so app knows when the song reached its end
NotificationCenter.default.addObserver(
self,
selector: #selector(itemDidFinishPlaying(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem)
//Assign playerItem to AVPlayer
songPlayer = AVPlayer(playerItem: playerItem)
if isSongPlaying {
print("Song at \(song.fileURL) stopped.")
isSongPlaying = false;
songPlayer.pause()
songPlayPauseImageView.image = UIImage(named: "play.png")
} else {
print("Song at \(song.fileURL) started.")
isSongPlaying = true;
songPlayer.play()
songPlayPauseImageView.image = UIImage(named: "pause.png")
}
}
When I start playing the song I get the message (as expected):
Song at file:///private/var/mobile/Containers/Data/Application/E34BE227-86DD-4AD7-A3C1-6A0610CE8C39/Library/Caches/CloudKit/6e22c352bc4bdeb0b75d42a0d681d67c69eeaad4/Assets/6BD84822-D039-4228-A5E9-59CB281BB964.0182fa97976e7d7b941bb8d8790ea75c6d3b72c302 started.
Unfortunately, the song doesn't play.