我有一首歌曲作为 CKAsset 存储在 CloudKit 中。我成功检索了这首歌并且没有出现错误,但它就是无法播放。任何帮助将不胜感激,谢谢。
我的 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开始了。
不幸的是,这首歌没有播放。