2017-02-17 229 views
0

我在主StoryBoard上設置了4個UIButtons。連續播放多個音頻文件

我想「Button4」實現連續的其他三個按鈕的方法。 (首先,播放音頻文件並設置選定的「Button1」,然後「Button2」,然後「Button3」)

然而,「Button2」和「Button3」同時播放。

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

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

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

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

      @IBAction func pushButton3(_ sender: UIButton) { 
       Player2(url: url1, url2: url2, url3: url3) 
      } 


      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 = true 
        player2!.play() 
        yourButton3.isSelected = true 
        player1!.play() 
       } 
      } 

      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 Player2(url: URL, url2: URL, url3: URL) { 
       do { 
        try player3 = AVAudioPlayer(contentsOf:url) 
        try player2 = AVAudioPlayer(contentsOf: url2) 
        try player1 = AVAudioPlayer(contentsOf: url3) 
        player3!.play() 
        yourButton.isSelected = true 
        player3!.delegate = self 
        player2!.delegate = self 
        player1!.delegate = self 
       } catch { 
        print(error) 
       } 
      } 
+0

你說,按下按鈕4,其他按鈕功能必須連續執行。這意味着每個音頻文件應該連續播放。如果這是你要找的東西,請檢查我的答案,並讓我知道你是否面臨任何問題 – KrishnaCA

回答

0

我相信,當你按下按鈕4,播放器1,應首先播放時,該玩家2後該玩家3.對於此任務後,下面的代碼應該工作。

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

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 
    if (player === player1) { 
     yourButton.isSelected = false 
     yourButton2.isSelected = true 
     do { 
      //initialise player 2 
      player2 = try AVAudioPlayer(contentsOf: url2) 
      player2!.delegate = self 
     }catch { 

     } 
     player2!.play() 
    } else if (player === player2) { 
     yourButton3.isSelected = true 
     yourButton2.isSelected = false 
     do { 
      //initialise player 3 
      player3 = try AVAudioPlayer(contentsOf: url3) 
      player3!.delegate = self 
     }catch { 

     } 
     player3!.play() 
    } else if (player === player3) { 
     yourButton3.isSelected = false 
    } 
} 
+0

我試過了你的代碼,但只播放了player1。 – risa8

+0

它看起來像player2,player 3沒有用音頻文件初始化。一旦你這樣做,他們將被播放。讓我編輯我的答案 – KrishnaCA

+0

@ risa8,嘗試更新的代碼 – KrishnaCA