var songList = NSMutableArray()
var player = AVPlayer()
var isSelected = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var songs = MPMediaQuery() // also tried var songs = MPMediaQuery.songsQuery()
var localSongs: NSArray = songs.items
songList = NSMutableArray(array: localSongs)
tableView.reloadData()
var song: MPMediaItem = songList[0] as MPMediaItem // <<- Error here
var currentItem = AVPlayerItem(URL: song.valueForProperty(MPMediaItemPropertyAssetURL) as NSURL)
player.replaceCurrentItemWithPlayerItem(currentItem)
player.play()
var songTitle: NSString = song.valueForProperty(MPMediaItemPropertyTitle) as NSString
songName.text = songTitle
var time = Float(CMTimeGetSeconds(player.currentItem.duration))
sliderOutlet.value = time
}
我正在构建一个音乐播放器,但是当我在模拟器中运行时应用程序崩溃并给出此错误:index 0 beyond bounds for empty array
,并在我尝试在 iPhone 上运行时说:{
MediaLibrary} Database validation succeeded
fatal error: unexpectedly found nil while unwrapping an Optional value
编辑:似乎它没有访问我 iPhone 上的本地音乐库并将歌曲添加到数组中。