如何在给定时间后激活功能?
How to activate a function after a given time?
我想在声音播放完成后激活一个功能。我假设需要包含 NSTimer,但我不确定如何实现它。如果您能给我一些有效的代码,那将很有帮助。
您应该 post 您尝试过的代码,然后有人可以帮助您使代码工作。在这种情况下,您不需要计时器来执行您正在做的事情。播放器上的 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")
}
}
我想在声音播放完成后激活一个功能。我假设需要包含 NSTimer,但我不确定如何实现它。如果您能给我一些有效的代码,那将很有帮助。
您应该 post 您尝试过的代码,然后有人可以帮助您使代码工作。在这种情况下,您不需要计时器来执行您正在做的事情。播放器上的 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")
}
}