没有调用Swift audioPlayerDidFinishPlaying方法

时间:2017-02-16 05:38:30

标签: ios swift xcode avaudioplayer

我在MainStoryBoard上创建了三个UIButtons。

当我按下“yourButton3”时,不会调用pl​​ayer3。

我发现如果将子句更改为else或者将某些内容分配给player3将解决此问题,但不知道如何。

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

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

        @IBOutlet weak var yourButton: customButton!
        @IBOutlet weak var yourButton2: customButton!
        @IBOutlet weak var yourButton3: 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) {
            Player(url: url2)
            player1!.play()

        }

//player3 is not called in this code

        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 = false
            }
        }



        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 Player3(url: URL, url2: URL) {
            do {
                try player1 = AVAudioPlayer(contentsOf:url)
                try player2 = AVAudioPlayer(contentsOf: url2)
                player1!.play()
                yourButton.isSelected = true
                player1!.delegate = self
            } catch {
                print(error)
            }
        }

1 个答案:

答案 0 :(得分:0)

您尚未在document.location.href函数中初始化player3,因此Player(url:)永远不会成为现实。

您可以按照以下方式修改player === player3功能:

Player3(url:, url2:)

并称之为:

func Player3(url: URL, url2: URL) {
    do {
        try player3 = AVAudioPlayer(contentsOf:url)
        try player2 = AVAudioPlayer(contentsOf: url2)
        player3!.play()
        yourButton.isSelected = true
        player3!.delegate = self
        player2!.delegate = self
    } catch {
        print(error)
    }
}