2017-02-18 190 views
1

我在主StoryBoard上創建了4個UIButton。如何播放多個音頻文件

我想「Button4」在一行中實現其他按鈕功能。這意味着當我按下按鈕4時,應該首先播放播放器1,播放器2之後以及播放器3之後播放。

但是,當我按下「Button4」時,同時播放「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) 
     } 

//"yourButton2" and "yourButton3" is played at same time in this code at player2 

     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

用UIAudio(UIAudio ...)鏈接UIAudio。編輯**重寫... –

回答

1

與使用AVAudioPlayer不同,AVQueuePlayer怎麼樣?

這裏有一個簡單的例子:

var files = ["file1", "file2", "file3"] 

var player: AVQueuePlayer = { 
    var pathArray = [String]() 
    files.forEach { resource in 
     if let path = Bundle.main.path(forResource: resource, ofType: "mp3") { 
      pathArray.append(path) 
     } 
    } 

    var urlArray = [URL]() 
    pathArray.forEach { path in 
     urlArray.append(URL(fileURLWithPath: path)) 
    } 

    var playerItems = [AVPlayerItem]() 
    urlArray.forEach { url in 
     playerItems.append(AVPlayerItem(url: url)) 
    } 

    let player = AVQueuePlayer(items: playerItems) 
    player.actionAtItemEnd = AVPlayerActionAtItemEnd.advance 

    return player 
}() 

,並在按鈕的動作:

@IBAction func buttonTapped(_ sender: UIButton) { 
    files = ["file2", "file3", "file1"] 
    player.play() 
} 

誠然,這是非常快速和骯髒的事,但那種喜歡這一點,因爲我們傳遞了文件陣列到播放器中。爲代碼找到更多優化應該不會太困難。

編輯:意識到我沒有傳入一個AVPlayerItems數組,所以更新。

+0

我會嘗試使用AVQueuePlayer,謝謝! – rebecca87

+0

沒問題。如果您有其他問題,請告訴我。 – jasonnoahchoi

+0

感謝它與AVQueuePlayer! – rebecca87

-1

編輯....

每個按鈕,1,2,3等等工作,但 「按鈕2」 是有點混的...

無論如何,重釋?