Implement micro-letter shake play sound, the code is as follows:
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event
{
if (motion = = Uieventsubtypemotionshake) {
NSLog (@ "Shake a Shake, hahaha");
[Self playsoundeffect:@ "Yaoyiyao"];
}
}
-(void) Playsoundeffect: (NSString *) name{
1. Get System sound ID
Systemsoundid Soundid;
/**
* Infileurl: Audio file URL
* Outsystemsoundid: Sound ID (This function adds the sound file to the System Audio service and returns a long shaping ID)
*/
NSString *audiofile=[[nsbundle Mainbundle] pathforresource:name oftype:@ "m4r"];
Nsurl *fileurl=[nsurl Fileurlwithpath:audiofile];
Audioservicescreatesystemsoundid ((__bridge cfurlref) (FILEURL), &soundid);
2. Play Audio
Audioservicesplaysystemsound (Soundid);
3. Play Vibration
Audioservicesplaysystemsound (ksystemsoundid_vibrate);
Audioservicesplayalertsound (Soundid)/play sound and vibrate
}
Here the Yaoyiyao is the sound name, the suffix name is. m4r, micro-mail shake the sound of the file can be downloaded online. In the simulator, can also like the real machine to achieve the same shaking mobile phone, operation: Hardware-->shake gesture.