我想在声音播放完成后激活一个功能。我认为需要包含NSTimer,但我不确定如何实现它。如果你能给我一些有效的代码,那将会很有帮助。
答案 0 :(得分:5)
您应该发布您尝试过的代码,然后有人可以帮助您使代码正常工作。在这种情况下,您不需要计时器来处理您正在做的事情。播放器上的AVAudioPlayerDelegate
协议是为您尝试做的而设计的:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer:AVAudioPlayer?
func playSound(fileName:NSString)
{
let path = NSBundle.mainBundle().pathForResource(fileName, ofType:"wav")
let url = NSURL.fileURLWithPath(path!)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
audioPlayer?.delegate = self
audioPlayer?.play()
} catch {
print("Player not available")
}
}
//MARK : AVAudioPlayerDelegate
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
print("finished playing, do something else here")
}
}