我正在开发一个swift的音乐应用程序,在尝试使用UITableViewRowAction播放专辑时遇到问题。我播放的第一张专辑效果很好,但是当我播放第二张专辑后,我得到了一个例外:
'NSRangeException',原因:'Index(0)超出界限(0)'
就行:
self.audioPlayer.nowPlayingItem = myCollectionToPlay.items[0]
我总是希望从第一首曲目播放滑动专辑。有没有人对为什么会这样做有任何建议?
let albumQuery: MPMediaQuery = MPMediaQuery.albums()
let audioPlayer = MPMusicPlayerController.systemMusicPlayer
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let play = UITableViewRowAction(style: .normal, title: "Play") { action, indexPath in
let albumTitle = self.allMusicInfo[indexPath.section].albums[indexPath.row].albumTitle
self.albumQuery.addFilterPredicate(MPMediaPropertyPredicate(
value: albumTitle,
forProperty: MPMediaItemPropertyAlbumTitle,
comparisonType: MPMediaPredicateComparison.equalTo
))
let myCollectionToPlay = MPMediaItemCollection(items: self.albumQuery.items!)
self.audioPlayer.setQueue(with: myCollectionToPlay)
self.audioPlayer.nowPlayingItem = myCollectionToPlay.items[0]
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}