如何逐个播放多个音频文件

时间:2017-02-18 05:24:07

标签: ios swift xcode

我在Main StoryBoard上创建了4个UIButtons。

我想" 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)
            }
        }

2 个答案:

答案 0 :(得分: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()
}

不可否认,这是非常快速和肮脏的,但有点像这样,因为我们将文件数组传递给播放器。找到这个代码的更多优化并不是很困难。

编辑:意识到我没有传递一个AVPlayerItem数组,所以更新了。

答案 1 :(得分:-1)

编辑...

每个按钮,1,2,3等......都可以工作,但是按下按钮2和#34;有点混合......

无论如何,重新解释?