我正在尝试在应用程序中构建一个音板,并找出了使用标签控制播放声音的有效方法。但是我现在想集成一个暂停按钮,可使用在AVAudioPlayer
的.stop()
方法一起使用,但是我得到一个错误与我当前的代码:Swift - 停止播放器
EXC_BAD_ACCESS
这是我使用的那一刻是什么,有任何想法吗?
import UIKit
import AVFoundation
let soundFilenames = ["sound","sound2","sound3"]
var audioPlayers = [AVAudioPlayer]()
class SecondViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
for sound in soundFilenames {
do {
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
audioPlayers.append(audioPlayer)
} catch {
//Catch error thrown
audioPlayers.append(AVAudioPlayer())
}
}
}
@IBAction func buttonPressed(sender: UIButton) {
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
@IBAction func stop(sender: UIButton) {
audioPlayer.stop()
}
}
可以工作......但只适用于播放的第一个声音文件,即与停止按钮具有相同标记的文件。我想停止按钮停止所有的声音,不管他们的标签。 – dwinnbrown