iosxcodeswiftswift2mpmediapickercontroller

How would I make a conditional statement for my mediaPickers?


I need a conditional statement so that I can select a song for one avplayer and another song for my other avplayer. I have two buttons that open the mediaPicker to select one song for each avplayer. The code I have currently works but the user has to select two songs for the avplayers and I would rather use two buttons instead. Here is the code I have currently:

if node.name == "addmusictoright" {
            mediaPicker2 = MPMediaPickerController(mediaTypes: .AnyAudio)

            if let picker = mediaPicker2{

                print("Successfully open media picker Left")
                picker.delegate = self
                picker.allowsPickingMultipleItems = true
                picker.showsCloudItems = false
                picker.prompt = "Please pick one song!"
                self.view?.window?.rootViewController!.presentViewController(picker, animated: true, completion: nil)

            } else {
                print("PICKER WONT OPEN")
}



if node.name == "addmusictoleft" {
            mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio)

            if let picker2 = mediaPicker{

                print("Successfully open media picker Left")
                picker2.delegate = self
                picker2.allowsPickingMultipleItems = true
                picker2.showsCloudItems = false
                picker2.prompt = "Please pick one song!"
                self.view?.window?.rootViewController!.presentViewController(picker2, animated: true, completion: nil)

            } else {
                print("PICKER WONT OPEN")
}




func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {

if mediaItemCollection.items.count == 2{

       let aMediaItem = mediaItemCollection.items[0] as MPMediaItem
       music = aMediaItem
       NSLog("\(aMediaItem.title)selected")
       let url: NSURL = (music.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!

        player =  AVPlayer(URL: url)
        titleLabel2.text = music.albumArtist



    let aMediaItem2 = mediaItemCollection.items[1] as MPMediaItem
    music2 = aMediaItem2
    NSLog("\(aMediaItem2.title)selected")
    let url2: NSURL = (music2.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!

    player2 =  AVPlayer(URL: url2)
    titleLabel.text = music2.albumArtist

}
}

Solution

  • We can do it a lot cleaner and better with using enums properly like :

    public enum NodeType: String {
       case right = "addmusictoright"
       case left = "addmusictoleft"
    
       func mediaPicker() -> (one: Bool, two: Bool, side: String) {
          switch self {
          case .right:
             return (one: false, two: true, side: "right")
          case .left:
             return (one: true, two: false, side: "left")
          }
       }
    }
    
    class GameScene: SKScene, MPMediaPickerControllerDelegate, AVAudioPlayerDelegate {
       var isMediaPickerOne = false
       var isMediaPickerTwo = false
       let nodeType = NodeType(rawValue: node.name)!
    
       mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio)
    
       if let picker = mediaPicker {
    
          isMediaPickerOne = nodeType.mediaPicker.one
          isMediaPickerTwo = nodeType.mediaPicker.two
    
          print("Successfully open media picker \(nodeType.mediaPicker.sode)")
          picker.delegate = self
          picker.allowsPickingMultipleItems = false
          picker.showsCloudItems = false
          picker.prompt = "Please pick one song!"
          self.view?.window?.rootViewController!.presentViewController(picker, animated: true, completion: nil)
       } else {
          print("PICKER WONT OPEN")
       }
    
       func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    
          isMediaPickerOne ? isMediaPickerTwo = !isMediaPickerOne: isMediaPickerOne = !isMediaPickerTwo
    
          let aMediaItem = mediaItemCollection.items[0] as MPMediaItem
          music = aMediaItem
          NSLog("\(aMediaItem.title)selected")
          let url: NSURL = (music.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!
    
          player = AVPlayer(URL: url)
          player.pause()
       }
    

    This will refactor a lot of duplicate code added in the question. This code might require some lil fixes to get compiled as i have not ran it on compiler. Hopefully it will work!