I got it to work but when I select the song to play the title of the song doesn't show up the first time. I have to go back to the media picker and select the songs again for the title of the song to show up. Why doesn't it work the first time I select the song? Also how would I remove the title of the song when I pick another song to play? Thank you! Here is the code I have:
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
if mediaItemCollection.items.count == 2{
mediaPicker.dismissViewControllerAnimated(true, completion: nil)
let aMediaItem = mediaItemCollection.items[0] as MPMediaItem
music = aMediaItem
NSLog("\(aMediaItem.title)selected")
let url: NSURL = (music.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!
do {
musicPlayer = try AVAudioPlayer(contentsOfURL: url)
} catch {
return
}
titleLabel2.text = music.title
titleLabel2 = SKLabelNode(fontNamed: "TimeBurner")
titleLabel2.fontColor = UIColor.whiteColor()
titleLabel2.zPosition = 40
titleLabel2.fontSize = 16
titleLabel2.position = CGPointMake(self.size.width / 5.3, self.size.height / 1.2)
addChild(titleLabel2)
let aMediaItem2 = mediaItemCollection.items[1] as MPMediaItem
music2 = aMediaItem2
NSLog("\(aMediaItem2.title)selected")
let url2: NSURL = (music2.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!
do {
musicPlayer2 = try AVAudioPlayer(contentsOfURL: url2)
} catch {
return
}
titleLabel.text = music2.title
titleLabel = SKLabelNode(fontNamed: "TimeBurner")
titleLabel.fontColor = UIColor.whiteColor()
titleLabel.zPosition = 40
titleLabel.fontSize = 16
titleLabel.position = CGPointMake(self.size.width / 1.3, self.size.height / 1.2)
addChild(self.titleLabel)
}
}