我创建了三个AVAudioPlayer,每个播放一个音频文件。在播放音频时,将UIButton设置为由audioPlayerDidFinishPlaying选择。
为了使用三个不同的AVAudioPlayer,我想只使用一个AVAudioPlayer来提高代码效率。
但是,当我尝试仅使用一个AVAudioPlayer重写代码时,无法使用audioPlayerDidFinishPlaying设置多个选定的UIButton。
我怎么解决这个问题?
这是带有三个不同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)
}
}
最佳答案
您可以使用整数来标识当前正在播放的不同文件。我修改了问题中给出的代码并将其写在下面。请看看并告诉我您是否有任何疑问
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)
}
}
关于ios - 仅使用一个AVAudioPlayer而不是三个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42346630/