我正在进行iOS键盘扩展。
要播放点击声,我使用AudioServicesPlaySystemSound
。
然而有些用户报告说,咔嗒声有时取决于铃声音量(铃声图标)'有时'正常的音量(扬声器图标)'
我在Apple备忘录应用程序上进行了测试,发现存在关于不稳定依赖的案例。
这是我的init
代码func initTypeSound(soundIndex: Int) {
let bundle = NSBundle.mainBundle()
for var i = 0 ; i < MAX_TYPE_SOUND_COUNT ; i++ {
if let url = bundle.URLForResource(NSString(format: "type%d_%d", soundIndex, i) as String, withExtension: "wav") {
// file exist
var soundID : SystemSoundID = 0
AudioServicesCreateSystemSoundID(url, &soundID)
mTypeSoundIDs.insert(soundID, atIndex: i)
} else {
// no file
}
}
}
和要播放的代码
func play(soundType: KKSoundType) {
if (!mHasPermission || !mIsSound) {
return
}
var session = AVAudioSession.sharedInstance()
let systemVolumn = session.outputVolume
if (systemVolumn == 0) {
return
}
var soundId : SystemSoundID!
switch (soundType) {
case .Type:
let rand = Int(arc4random_uniform(UInt32(mTypeSoundIDs.count)));
soundId = mTypeSoundIDs[rand];
break
case .Space:
soundId = mSpaceSoundID;
break
default:
return
}
if mIsSound {
AudioServicesPlaySystemSound(soundId)
}
}