System sound & warning sound & vibration, menghua system sound
In order to attract the user's attention when issuing a warning, it is often accompanied by a prompt sound vibration. the system audio service provides an interface for playing audio files of no more than 30 seconds. The supported formats include CAF, AIF, and WAV.
IOS uses this API to support three types of notifications:
Sound: play a simple sound file immediately. If the phone is muted, the user cannot hear anything.
Reminder: the audio file is played. If the mobile phone is set to mute or vibrate, the user will be notified through vibrate.
Shake: Shake the phone. Do not consider other settings.
First, import the framework: AudioToolbox. import the framework file in the file as an excuse: # import <AudioToolbox/AudioToolbox. h>
Add three buttons to the xib file to implement the above three functions. Put the audio file in WAV format
// Sound
-(IBAction) buttonClick :( UIButton *) sender {// The warning sound is generally short. // declare the variable soundid to introduce the SystemSoundID soundid of the music file; // obtain the audio file path NSString * soundfile = [[NSBundle mainBundle] pathForResource: @ "soundeffect" ofType: @ "wav"]; // 1. the CFURLRef pointing to the file location. 2. pointer // _ bridge. convert the C language structure to the OC language object AudioServicesCreateSystemSoundID (_ bridge CFURLRef) [NSURL fileURLWithPath: soundfile], & soundid); // play the warning audio AudioServicesPlaySystemSound (soundid );}
// Reminder
-(IBAction) alertButtonClick :( UIButton *) sender {SystemSoundID soundid; NSString * soundfile = [[NSBundle mainBundle] pathForResource: @ "soundeffect" ofType: @ "wav"]; translate (_ bridge CFURLRef) [NSURL fileURLWithPath: soundfile], & soundid); // warning tone. When the user changes to mute, AudioServicesPlayAlertSound (soundid );}
// Vibrate
// Vibration-(IBAction) shakeButtonClick :( UIButton *) sender {SystemSoundID soundid; // no matter the mobile phone status, only the vibration AudioServicesPlaySystemSound (soundid );}