仅使用一个AVAudioPlayer而不是三个

时间:2017-02-20 14:02:34

标签: ios swift audio

我创建了三个AVAudioPlayer,每个播放一个音频文件。正在播放音频时,音频播放器设置了由音频播放器设置的音频播放器。

考虑到使用三个不同的AVAudioPlayer,我想只使用一个AVAudioPlayer来提高代码效率。

但是当我尝试用一​​个AVAudioPlayer重写代码时,我无法使用audioPlayerDidFinishPlaying设置多个UIButtons。

我该如何解决这个问题?

以下是具有三个不同AVAudioPlayer的代码。

        let url1 = Bundle.main.bundleURL.appendingPathComponent("audioFile1.mp3")
        let url2 = Bundle.main.bundleURL.appendingPathComponent("audioFile2.mp3")
        let url3 = Bundle.main.bundleURL.appendingPathComponent("audioFile3.mp3")


        @IBOutlet weak var yourButton1: 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) {
            audioPlayer1(url: url1)
        }

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

        @IBAction func pushButton3(_ sender: UIButton) {
            audioPlayer3(url: 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) {
                yourButton3.isSelected = true
            }
        }

        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) {
            do {
                try player3 = AVAudioPlayer(contentsOf:url)
                player3!.play()
                yourButton3.isSelected = true
                player3!.delegate = self

            } catch {
                print(error)
            }
        }

1 个答案:

答案 0 :(得分:2)

您可以使用整数来识别当前正在播放的不同文件。我修改了问题中给出的代码,并在下面编写。请看看它并告诉我你是否有任何疑问

let url1 = Bundle.main.bundleURL.appendingPathComponent("audioFile1.mp3")
let url2 = Bundle.main.bundleURL.appendingPathComponent("audioFile2.mp3")
let url3 = Bundle.main.bundleURL.appendingPathComponent("audioFile3.mp3")

@IBOutlet weak var yourButton1: UIButton!
@IBOutlet weak var yourButton2: UIButton!
@IBOutlet weak var yourButton3: UIButton!

fileprivate var lonelyPlayer:AVAudioPlayer?
var currentPlayer: Int = 0

@IBAction func pushButton1(sender: UIButton) {
    if self.lonelyPlayer == nil {

    }else {
        if (self.lonelyPlayer?.isPlaying)! {
            self.lonelyPlayer?.stop()
        }
    }
    audioPlayer(url: url1, buttonId: 1)
}

@IBAction func pushButton2(sender: UIButton) {
    if self.lonelyPlayer == nil {

    }else {
        if (self.lonelyPlayer?.isPlaying)! {
            self.lonelyPlayer?.stop()
        }
    }
    audioPlayer(url: url2, buttonId: 2)
}

@IBAction func pushButton3(_ sender: UIButton) {
    if self.lonelyPlayer == nil {

    }else {
        if (self.lonelyPlayer?.isPlaying)! {
            self.lonelyPlayer?.stop()
        }
    }
    audioPlayer(url: url3, buttonId: 3)
}

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    if (self.currentPlayer == 1) {
        yourButton1.isSelected = false
    } else if (self.currentPlayer == 2) {
        yourButton2.isSelected = false
    } else if (self.currentPlayer == 3) {
        yourButton3.isSelected = false
    }
}

func audioPlayer(url: URL, buttonId: Int) {
    do {
        try lonelyPlayer = AVAudioPlayer(contentsOf:url)
        lonelyPlayer!.play()
        lonelyPlayer!.delegate = self
        yourButton1.isSelected = (buttonId == 1)
        yourButton2.isSelected = (buttonId == 2)
        yourButton3.isSelected = (buttonId == 3)
        currentPlayer = buttonId
    } catch {
        print(error)
    }
}