2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
It is mainly divided into 3 steps:
AudioServicesCreateSystemSoundID
Create system sounds.AudioServicesAddSystemSoundCompletion
Set callback.AudioServicesPlaySystemSound
Start playing.about AudioServicesPlaySystemSound
function:
This function plays a short sound (duration 30 seconds or less). Because a sound may play for several seconds, this function executes asynchronously. To find out when the sound has finished playing, call AudioServicesAddSystemSoundCompletion
Function to register callback function.
On some iOS devices, you can AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
To invoke vibration. On other iOS devices, calling this function with this constant has no effect.
The following are the restrictions on the audio files that this function can play:
.caf
, .aif
, or .wav
fileBut in fact, AAC files can also be played normally. In addition, this function has many limitations:
Core code:
- (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;
}
Project address: https://github.com/UestcXiye/AudioToolboxSystemSound.