我如何为mediaPickers制作条件声明?

时间:2016-04-26 18:01:34

标签: ios xcode swift swift2 mpmediapickercontroller

我需要一个条件语句,以便为一个avplayer选择一首歌,为另一个avplayer选择另一首歌。我有两个按钮打开mediaPicker为每个avplayer选择一首歌曲。我目前使用的代码但用户必须为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

}
}

2 个答案:

答案 0 :(得分:2)

我们可以使用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修复才能编译,因为我没有在编译器上运行它。 希望它会起作用!

答案 1 :(得分:0)

好的,所以我终于在这里工作了我是怎么做到的:

[i:j:k]