在 Swift 中使用 AV 音频播放器有条件地播放音频
Conditional audio playing using AV Audio Player in Swift
我希望最多可以按两次按钮来播放音频。按下按钮两次后,即使按下也不会再播放音频。我目前有这段代码,但它不起作用,我不确定我哪里出错了:
var soundFileURLRef: NSURL!
var audioPlayer = AVAudioPlayer?()
var audioCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
// setup for audio
let playObjects = NSBundle.mainBundle().URLForResource("mathsinfo", withExtension: "mp3")
self.soundFileURLRef = playObjects
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: soundFileURLRef)
} catch _ {
audioPlayer = nil
}
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
}
//function for counting times audio is played
func countAudio() {
if ((audioPlayer?.play()) != nil) {
++audioCounter
}
}
//MARK: Actions
//action for button playing the audio
@IBAction func playMathsQuestion(sender: AnyObject) {
countAudio()
if audioCounter < 2 {
audioPlayer?.play()
}
}
您代码中的 countAudio
函数会产生异步播放声音的副作用,因为您调用了 audioPlayer?.play()
。 audioCounter
变量仅在 audioPlayer
为 nil 时递增。试试这个版本
var soundFileURLRef: NSURL!
var audioPlayer: AVAudioPlayer?
var audioCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
// setup for audio
let playObjects = NSBundle.mainBundle().URLForResource("mathsinfo", withExtension: "mp3")
self.soundFileURLRef = playObjects
audioPlayer = try? AVAudioPlayer(contentsOfURL: soundFileURLRef)
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
}
//MARK: Actions
//action for button playing the audio
@IBAction func playMathsQuestion(sender: AnyObject) {
if (audioPlayer != nil) {
if (audioCounter < 2 && audioPlayer!.play()) {
++audioCounter
}
}
}
可以看到这里我们先检查确保audioPlayer
不为nil。之后我们只在 audioCounter < 2
时执行 audioPlayer!.play()
调用。调用 play()
returns 一个布尔值,当音频成功排队等待播放时为真(请参阅 documentation),在这种情况下我们递增 audioCounter
.
我还通过使用 try?
版本的 try 简化了 audioPlayer
的初始化,当被调用者抛出 returns nil
时
我希望最多可以按两次按钮来播放音频。按下按钮两次后,即使按下也不会再播放音频。我目前有这段代码,但它不起作用,我不确定我哪里出错了:
var soundFileURLRef: NSURL!
var audioPlayer = AVAudioPlayer?()
var audioCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
// setup for audio
let playObjects = NSBundle.mainBundle().URLForResource("mathsinfo", withExtension: "mp3")
self.soundFileURLRef = playObjects
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: soundFileURLRef)
} catch _ {
audioPlayer = nil
}
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
}
//function for counting times audio is played
func countAudio() {
if ((audioPlayer?.play()) != nil) {
++audioCounter
}
}
//MARK: Actions
//action for button playing the audio
@IBAction func playMathsQuestion(sender: AnyObject) {
countAudio()
if audioCounter < 2 {
audioPlayer?.play()
}
}
您代码中的 countAudio
函数会产生异步播放声音的副作用,因为您调用了 audioPlayer?.play()
。 audioCounter
变量仅在 audioPlayer
为 nil 时递增。试试这个版本
var soundFileURLRef: NSURL!
var audioPlayer: AVAudioPlayer?
var audioCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
// setup for audio
let playObjects = NSBundle.mainBundle().URLForResource("mathsinfo", withExtension: "mp3")
self.soundFileURLRef = playObjects
audioPlayer = try? AVAudioPlayer(contentsOfURL: soundFileURLRef)
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
}
//MARK: Actions
//action for button playing the audio
@IBAction func playMathsQuestion(sender: AnyObject) {
if (audioPlayer != nil) {
if (audioCounter < 2 && audioPlayer!.play()) {
++audioCounter
}
}
}
可以看到这里我们先检查确保audioPlayer
不为nil。之后我们只在 audioCounter < 2
时执行 audioPlayer!.play()
调用。调用 play()
returns 一个布尔值,当音频成功排队等待播放时为真(请参阅 documentation),在这种情况下我们递增 audioCounter
.
我还通过使用 try?
版本的 try 简化了 audioPlayer
的初始化,当被调用者抛出 returns nil
时