2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
主要分为 3 步:
AudioServicesCreateSystemSoundID
创建系统声音。AudioServicesAddSystemSoundCompletion
设置回调。AudioServicesPlaySystemSound
开始播放。关于 AudioServicesPlaySystemSound
函数:
此功能播放短声音(持续时间为 30s 或更短)。由于声音可能会播放几秒钟,此功能是异步执行的。要了解声音何时完成播放,调用 AudioServicesAddSystemSoundCompletion
函数来注册回调函数。
在某些 iOS 设备上,可以通过 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
来调用振动。在其他 iOS 设备上,使用该常量调用此函数没有任何作用。
以下是该函数能播放的音频文件的限制:
.caf
, .aif
, or .wav
file但实际上,AAC 文件也是能正常播放的,此外,该函数还有诸多限制:
核心代码:
- (void)btnClick:(UIButton *)sender
{
self.mButton.hidden = YES;
NSURL *audioUrl = [[NSBundle mainBundle] URLForResource:@"music" withExtension:@"aac"];
SystemSoundID soundID;
// create a system sound object
AudioServicesCreateSystemSoundID((__bridge CFURLRef)audioUrl, &soundID);
// register a callback function that is invoked when a specified system sound finishes playing
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, &playCallback, (__bridge void *_Nullable)(self));
// play a system sound object
AudioServicesPlaySystemSound(soundID);
}
void playCallback(SystemSoundID ssID, void *clientData)
{
ViewController *vc = (__bridge ViewController *)clientData;
vc->_mButton.hidden = NO;
}
项目地址:https://github.com/UestcXiye/AudioToolboxSystemSound。