-1

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)

    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
coding22
  • 689
  • 1
  • 7
  • 28
  • Its hard to tell without more context but your issue may be that you need to explicitly dispatch your UI updates to the main thread, since the UI resides on the main thread. You do this by wrapping your calls to anything regarding a change to the UI with `dispatch_async(dispatch_get_main_queue()) {//perform UI updates here }`. If you are unfamiliar with Grand Central Dispatch then [here](https://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1) is a great Swift tutorial – MikeG Apr 17 '16 at 01:42
  • I got it to work by calling the labels in the didMoveToView and displaying the text of the title after I selected the songs. – coding22 Apr 17 '16 at 02:04
  • @MikeG Would you know how to remove the labels when the user selects another song? Right now if I select another song it just overlaps the previous song title. – coding22 Apr 17 '16 at 02:06
  • 1
    nevermind I got it to work. – coding22 Apr 17 '16 at 02:26

1 Answers1

0

Heres how I got it to work. I added the title labels to the didmovetoview and added the text to the didPickMediaItems.

override func didMoveToView(view: SKView) {
titleLabel2 = SKLabelNode(fontNamed: "TimeBurner")
titleLabel2.fontColor = UIColor.whiteColor()
titleLabel2.zPosition = 40
titleLabel2.fontSize = 16
titleLabel2.position = CGPointMake(self.size.width / 6.0, self.size.height / 1.25)
addChild(titleLabel2)


titleLabel = SKLabelNode(fontNamed: "TimeBurner")
titleLabel.fontColor = UIColor.whiteColor()
titleLabel.zPosition = 40
titleLabel.fontSize = 16
titleLabel.position = CGPointMake(self.size.width / 1.2, self.size.height / 1.25)
addChild(self.titleLabel)

}


  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



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
addChild(self.titleLabel)

}
}
coding22
  • 689
  • 1
  • 7
  • 28