SoFunction
Updated on 2025-04-12

Summary of the use of prompt sounds for IOS playback system (AudioToolbox)

Summary of the use of prompt sounds for IOS playback system (AudioToolbox)

During the development process, Apple's own system prompt sound is required. Below I have summarized the method of playing system prompt sounds.

The first step is to import the AudioToolbox framework

#import <AudioToolbox/>

Play the prompt sounds that come with the system

The prompt sounds that come with the playback system are very simple, and you only need two lines of code to complete it:

//Define a SystemSoundID SystemSoundID soundID = 1000;//The specific parameters are posted below //Play the sound AudioServicesPlaySystemSound(soundID);

About SystemSoundID's related parameters and introduction to all the system ringtones

Play custom prompt sounds, both sound and vibration

- (void)playNotifySound {
 //Get the path NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
 //Define a SystemSoundID SystemSoundID soundID;
 //Judge whether the path exists if (path) {
  //Create an audio file to playback system sound server  OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &amp;soundID);
  //Judge whether there is any error  if (error != kAudioServicesNoError) {
   NSLog(@"%d",(int)error);
  }
 }
 //Play sound and vibration AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
  //The playback successfully callback });
}

Only vibration, no sound

 //The phone only vibrates without sound AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Only sound without vibration

//It must be a custom sound, and the sounds of the test system seem to be vibrating.- (void)playNotifySound {
 //Get the path NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
 //Define a SystemSoundID with vibration SystemSoundID soundID = 1000;
 //Judge whether the path exists if (path) {
  //Create an audio file to playback system sound server  OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &amp;soundID);
  //Judge whether there is any error  if (error != kAudioServicesNoError) {
   NSLog(@"%d",(int)error);
  }
 }
 //Only play sound, no vibration AudioServicesPlaySystemSound(soundID);
}

The above are some tips for using prompt sounds. I hope you can learn something. If there are any shortcomings, I hope you will give it a supplement. Thank you for reading!