2017-02-16 21 views
0

MainStoryBoardに3つのUIButtonを作成しました。Swift audioPlayerDidFinishPlayingメソッドが呼び出されていません

"yourButton3"を押すと、player3は呼び出されません。

私は、else節を変更したり、player3に何かを割り当てると、この問題を解決することができますが、その方法はわかりません。

 override func viewDidLoad() { 
      super.viewDidLoad() 
      player1?.delegate = self 
      player2?.delegate = self 
      player3?.delegate = self 
     } 

     let url1 = Bundle.main.bundleURL.appendingPathComponent("music1.mp3") 
     let url2 = Bundle.main.bundleURL.appendingPathComponent("music2.mp3") 

     @IBOutlet weak var yourButton: customButton! 
     @IBOutlet weak var yourButton2: customButton! 
     @IBOutlet weak var yourButton3: customButton! 


     fileprivate var player1:AVAudioPlayer? 
     fileprivate var player2:AVAudioPlayer? 
     fileprivate var player3:AVAudioPlayer? 


     @IBAction func pushButton1(sender: UIButton) { 
      Player(url: url1) 
     } 

     @IBAction func pushButton2(sender: UIButton) { 
      Player1(url: url2) 
     } 

     @IBAction func pushButton3(_ sender: UIButton) { 
      Player(url: url2) 
      player1!.play() 

     } 

//player3 is not called in this code 

     func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 
      if (player === player1) { 
       yourButton.isSelected = false 
      } else if (player === player2) { 
       yourButton2.isSelected = false 
      } else if (player === player3) { 
       yourButton.isSelected = false 
       player2!.play() 
       yourButton2.isSelected = false 
      } 
     } 



     func Player(url: URL) { 
      do { 
       try player1 = AVAudioPlayer(contentsOf:url) 
       player1!.play() 
       yourButton.isSelected = true 
       player1!.delegate = self 
      } catch { 
       print(error) 
      } 
     } 

     func Player1(url: URL) { 
      do { 
       try player2 = AVAudioPlayer(contentsOf:url) 
       player2!.play() 
       yourButton2.isSelected = true 
       player2!.delegate = self 

      } catch { 
       print(error) 
      } 
     } 

     func Player3(url: URL, url2: URL) { 
      do { 
       try player1 = AVAudioPlayer(contentsOf:url) 
       try player2 = AVAudioPlayer(contentsOf: url2) 
       player1!.play() 
       yourButton.isSelected = true 
       player1!.delegate = self 
      } catch { 
       print(error) 
      } 
     } 

答えて

0

はあなたがあなたのPlayer(url:)機能ではないのinit player3を持っているので、player === player3は真ではありません。

あなたは以下のようにあなたのPlayer3(url:, url2:)機能を編集することができます。

func Player3(url: URL, url2: URL) { 
    do { 
     try player3 = AVAudioPlayer(contentsOf:url) 
     try player2 = AVAudioPlayer(contentsOf: url2) 
     player3!.play() 
     yourButton.isSelected = true 
     player3!.delegate = self 
     player2!.delegate = self 
    } catch { 
     print(error) 
    } 
} 

とそのようにそれを呼び出す:それがうまく働いた

@IBAction func pushButton3(_ sender: UIButton) { 
    Player3(url: url1, url2: url2) 
} 
+0

感謝を! – johnCole

+0

あなたが次の質問に私を助けることができればそれはいいと思います! http://stackoverflow.com/questions/42271453/how-to-multiple-play-in-audioplayerdidfinishplaying-method – johnCole

関連する問題