#import <AudioToolbox/AudioToolbox.h>
-(void) viewdidload {
[Super viewdidload];
additional setup after loading the view, typically from a nib.
// in IOS , audio includes short-acting audio and music , the implementation of short- acting audio is System Audiotoolbox, music playback uses Avfoundation frame
//Short -Acting audio : The playback process is not controllable and lasts no more than a few seconds
// Define a system sound ID
systemsoundid soundid;
// set up resources to play short-acting audio
Audioservicescreatesystemsoundid (__bridge cfurlref _nonnull) ([[NSBundle mainbundle] urlforresource:@ "Shake" withextension:@ "wav"] ), &soundid);
// Set the effect of vibration , need the real machine
Audioservicesplayalertsound (ksystemsoundid_vibrate);
audioservicesplaysystemsound(ksystemsoundid_vibrate);
// Play short-acting audio
audioservicesplaysystemsound(soundid);
Very simply, you just need to let the controller itself support shaking//and make him the first corresponding person:
[[Uiapplicationsharedapplication] setapplicationsupportsshaketoedit:yes]; [Selfbecomefirstresponder];
}
shake a shake depends on the principle of gravity induction
-(void) Motionbegan: (uieventsubtype) motion withevent: (uievent *) event {
// Define a system sound ID
systemsoundid soundid;
// set up resources to play short-acting audio
Audioservicescreatesystemsoundid (__bridge cfurlref _nonnull) ([[NSBundle mainbundle] urlforresource:@ "Shake" withextension:@ "wav"] ), &soundid);
// Set the effect of vibration , need the real machine
//Audioservicesplayalertsound (ksystemsoundid_vibrate);
audioservicesplaysystemsound(ksystemsoundid_vibrate);
// Play short-acting audio
audioservicesplaysystemsound(soundid);
NSLog(@ " Shake start ");
}
-(void) motionended: (uieventsubtype) motion withevent: (uievent *) event {
NSLog(@ " Shake end ");
}
-(void) motioncancelled: (uieventsubtype) motion withevent: (uievent *) event {
NSLog(@ " Shake cancel ");
}
Shake a Shake