Chirp
在您的 Swift 应用中准备、播放和删除声音的最简单方法!
## 安装 ### CocoaPods 安装 Chirp 可在 CocoaPods 上找到。只需将以下内容添加到您的项目 Podfile 中
pod 'Chirp', '~> 1.1'
### 非CocoaPods 安装 您可以直接将 Chirp.swift 添加到项目中,或将 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
}
享受吧!我知道声音管理可能会有些麻烦。希望这能对您的项目有所帮助。