使用AVAudioPlayer播放多个音频文件

时间:2017-02-19 06:50:09

标签: ios swift xcode avaudioplayer

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

当我按下" Button1"或" Button2"每个播放一个音频文件("播放器1","播放器2"。),当播放音频时,UIButton设置为selected

我想" Button3"连续实现多个功能。 这意味着当我按下"按钮3","播放器3"应该先玩,然后玩#1;玩家1"之后"球员2"。

然而,当我按下" Button3","播放器1"和"球员2"同时播放,因为前一个完成没有延迟。

我发现设置AVAudioPlayer的委托或使用AVQueuePlayer可以解决问题,但我发现很难进行更改。

I created image to make it easier to understand

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


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

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

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


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

            //"player 1" and "player 2" are played at same time

            yourButton3.isSelected = false
            player1!.play()
            yourButton1.isSelected = true
            player2!.play()
            yourButton2.isSelected = false
        }
    }


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

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

        } catch {
            print(error)
        }
    }

    func audioPlayer3(url: URL, url2: URL, url3: URL) {
        do {
            try player3 = AVAudioPlayer(contentsOf:url3)
            try player1 = AVAudioPlayer(contentsOf: url1)
            try player2 = AVAudioPlayer(contentsOf: url2)
            player3!.play()
            yourButton3.isSelected = true
            player3!.delegate = self
            player1!.delegate = self
        } catch {
            print(error)
        }
    }
}

更改了代码

    var queue = AVQueuePlayer()
    var items = [AVPlayerItem]()


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

        let asset1 = AVPlayerItem(url: url1)
        let asset2 = AVPlayerItem(url: url2)
        let asset3 = AVPlayerItem(url: url3)
        let asset4 = AVPlayerItem(url: url4)

        items = [asset1, asset2, asset3, asset4]

        queue = AVQueuePlayer(items: items)
        for item in queue.items() {
            NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object: item)
        }

    }
 @IBAction func pushButton3(_ sender: UIButton) {
        //audioPlayer3(url: url1, url2: url2)
        sender.isSelected = true
        queue.play()
    }
 func playerItemDidReachEnd(notification: NSNotification) {

        if notification.object as? AVPlayerItem == items[0] {
            yourButton3.isSelected = false
            yourButton1.isSelected = true
        }

        if notification.object as? AVPlayerItem == items[1] {
            yourButton1.isSelected = false
            yourButton2.isSelected = true
        }

        if notification.object as? AVPlayerItem == items[2] {
            yourButton2.isSelected = false
            yourButton4.isSelected = true
            //yourButton1.isSelected = true
        }

        if notification.object as? AVPlayerItem == items[3] {
            yourButton4.isSelected = false
            print("item 3")
        }

1 个答案:

答案 0 :(得分:1)

编辑:This is the method AVAudioPlayer

基本上如此:

1:只添加一名玩家,移除剩余的玩家。

2: Create an NSMutableArray包含您要播放的对象。

3:根据按下的按钮,您可以重新排列NSMutableArray个对象,以根据需要设置正确的顺序。 (如果你想让它变得简单,只需重新创建数组)然后开始播放数组中的firstObject(url),将其添加到你的播放器中。

4:当玩家完成游戏时,您可以开始播放下一个对象,从NSMutableArray以与上面相同的方式将其添加到您的播放器。

遵循上述步骤将避免让多个玩家同时玩,以及其他好处(例如避免同时加载多个网址等)

您可能需要一些变量来检查currentPlayingObject以确定下一个是哪一个,可能是int,并检查您是否尝试从阵列加载新项目(如果它是最后一个)项目播放,以避免访问不存在的objectAtIndex时崩溃。