How to multiple play() in audioPlayerDidFinishPlaying method

余生长醉 提交于 2019-12-02 06:42:37

问题


When I press "yourButton" or "yourButton2" or "yourButton3" each plays an audio file and while the audio is playing the UIButton is set selected.

I would like "yourButton4" to implement the method of the other UIButtons in a row. (At first, plays an audio file and set selected "yourButton" then "yourButton2" and "yourButton3") .

However, when I press "yourButton4", "yourButton2" and "yourButton3" is played at same time as play() method Plays a sound asynchronously.

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


        @IBOutlet weak var yourButton: customButton!
        @IBOutlet weak var yourButton2: customButton!
        @IBOutlet weak var yourButton3: customButton!
        @IBOutlet weak var yourButton4: 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) {
            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)
            }
        }

回答1:


Don't use this 3 different in VC

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

Instead of this use only one of this in AppDelegate and access it from your VC so you won't have same issue. When next audio will play previous audio will stop

Use this in App delegate:

var player1:AVAudioPlayer?

Again in your VC use this instance

func Player1(url: URL) {
            do {
 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                try appDelegate.player1 = AVAudioPlayer(contentsOf:url)
                appDelegate.player1!.play()
                yourButton2.isSelected = true
                appDelegate.player1!.delegate = self

            } catch {
                print(error)
            }
        }



回答2:


You are playing all at the same time, there is no delay or nothing waiting for the previous to finish.

Your class should become the delegate of the AVAudioPlayer, so you first play the first audio file and once you get the delegate call audioPlayerDidFinishPlaying:successfully: you play the next one.

You should modify your delegate call to look something like this:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
            if (player === player1) {
                player2!.play()
            } else if (player === player2) {
                player3!.play()
            } else if (player === player3) {
                // You're done!
            }
        }


来源:https://stackoverflow.com/questions/42271453/how-to-multiple-play-in-audioplayerdidfinishplaying-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!