Search code examples
iosswiftxcodeavaudioplayer

How to multiple play() in audioPlayerDidFinishPlaying method


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)
            }
        }

Solution

  • 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)
                }
            }