Swift app聲音管理庫 Chirp,可以通過簡便的方式加載、播放、移除聲音。
示例代碼
prepareSound
用于將聲音預(yù)加載到內(nèi)存中。 這會(huì)將聲音的擁有數(shù)量增加1。在調(diào)用playSound
之前必須調(diào)用此方法。
/* 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
播放預(yù)加載的聲音。
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
從內(nèi)存中移除聲音。
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 }