我如何为我的 mediaPickers 做一个条件语句?

How would I make a conditional statement for my mediaPickers?

我需要一个条件语句,这样我就可以 select 一个 avplayer 唱一首歌,另一个 avplayer 唱另一首歌。我有两个按钮可以打开 mediaPicker select 一首歌曲 avplayer。我目前使用的代码可以工作,但用户必须 select 两首歌曲才能 avplayers,我宁愿使用两个按钮。这是我目前的代码:

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

}
}

好吧,我终于让它工作了,我是这样做的:

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

}
   }

我们可以通过正确使用 enums 来做得更干净、更好,例如 :

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

这将重构问题中添加的大量重复代码。这段代码可能需要一些 lil 修复才能编译,因为我没有 运行 它在编译器上。 希望它能奏效!