我将一首歌存储在CloudKit中作为CKAsset。我成功检索了这首歌并没有得到任何错误,但它不会播放。任何帮助将不胜感激,谢谢。
我的AVPlayer在课堂上懒惰地实例化。
//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")
}
}
当我开始播放歌曲时,我收到了消息(正如预期的那样):
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已开始。
不幸的是,这首歌没有播放。