Swift app声音管理库 Chirp,可以通过简便的方式加载、播放、移除声音。
示例代码
prepareSound用于将声音预加载到内存中。 这会将声音的拥有数量增加1。在调用playSound之前必须调用此方法。
/* MyViewController.swift */
override func viewDidLoad() {
super.viewDidLoad()
// Load sounds into memory
Chirp.sharedManager.prepareSound("boop") // default extension is .wav
Chirp.sharedManager.prepareSound("ding.mp3") // so other extensions you must name explicitly
}
通过playSound 播放预加载的声音。
func submitButtonTouched(button: UIButton) {
// Since the sound is already loaded into memory, this will play immediately
Chirp.sharedManager.playSound("boop")
// example function that might get called when you touch a button
submitForm()
}
通过removeSound从内存中移除声音。
deinit {
// Cleanup is really simple!
Chirp.sharedManager.removeSound("boop")
Chirp.sharedManager.removeSound("ding.mp3")
Chirp.sharedManager.removeSound("oops.mp3")
// If you never loaded the sounds, e.g. viewDidLoad wasn't called, or submission never failed or succeeded,
// that's ok, because these will function as no-ops
}