0

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

}
}
coding22
  • 689
  • 1
  • 7
  • 28
  • Are you talking about this conditional statement if mediaItemCollection.items.count == 2{ } Little unclear do you want to have two buttons or just one button . – Sourav Sachdeva Apr 27 '16 at 06:57
  • Well that conditional statement allows me to pick two songs for my `avplayers` from one button. What I want is to use two buttons and pick one song each. – coding22 Apr 27 '16 at 07:00
  • Can you show a little flow , how you are using a single button to pick two songs , i.e. what's the name of function that gets called when you click on that button (code that runs on button click ) before opening the media picker . I'm not quite getting the node.name == "addmusictoright" thingy .How your mediaItemCollection.items.count == 2 is returning true , are you opening both media pickers simultaneously on that button click. – Sourav Sachdeva Apr 27 '16 at 08:06
  • Okay so Im currently using one button that opens the mediaPicker and the user picks two songs for the avplayers. What I want is to use two buttons thats why I included the `addmusictoright` and `addmusictoleft`. So for example I want to press on one button and it opens the mediaPicker and selects one song for one of the `avplayer and I then press on another button to open the mediapicker and select another song for the other `avplayer`. The `node.name == "addmusictoright"` code allows me to make my node into a button. – coding22 Apr 27 '16 at 17:02
  • @SouravSachdeva Do you get what im tryin to accomplish? – coding22 Apr 27 '16 at 17:40
  • I didn't get the code allows me to make my node into button , but from what i have got is you need to make Two IBActions for buttons . you can call your methods addmusictoleft() and addmusictoRight() if you want to. Can you show me the relevant code of controller . – Sourav Sachdeva Apr 27 '16 at 17:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110419/discussion-between-sourav-sachdeva-and-coding22). – Sourav Sachdeva Apr 27 '16 at 18:21
  • @SouravSachdeva Hey are you there I added some info in the chat? – coding22 Apr 27 '16 at 19:11

2 Answers2

2

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!

Muhammad Ali
  • 2,173
  • 15
  • 20
0

Okay so I finally got it to work here is how I did it:

class GameScene: SKScene, MPMediaPickerControllerDelegate, AVAudioPlayerDelegate {
var isMediaPickerOne = false
var isMediaPickerTwo = false

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

        if let picker = mediaPicker2{

           isMediaPickerTwo = true
           isMediaPickerOne = false

            print("Successfully open media picker Left")
            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")
}



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

        if let picker2 = mediaPicker{

                isMediaPickerOne = true
                isMediaPickerTwo = false


            print("Successfully open media picker Left")
            picker2.delegate = self
            picker2.allowsPickingMultipleItems = false
            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 isMediaPickerOne {
isMediaPickerTwo = false


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()
}




if isMediaPickerTwo {
isMediaPickerOne = false

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

player2 =  AVPlayer(URL: url2)
player2.pause()

}
   }
coding22
  • 689
  • 1
  • 7
  • 28