ios - Performing actions from accelerometer -


i'm new programming in xcode , have hit brick wall. trying sound play if there sufficient acceleration detected. can please advise me on doing wrong? (i suspect everything).

code:

#import "lzdviewcontroller.h"  @interface lzdviewcontroller ()  @end  @implementation lzdviewcontroller  - (void)viewdidload;{  [super viewdidload]; // additional setup after loading view, typically nib. cmaccelerometerdata *accelerometerdata = self.motionmanager.accelerometerdata; if (fabsf(accelerometerdata.acceleration.x) > 1.2     || fabsf(accelerometerdata.acceleration.y) > 1.2     || fabsf(accelerometerdata.acceleration.z) > 1.2) {     nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/audio.wav", [[nsbundle mainbundle] resourcepath]]];     audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:nil];     audioplayer.numberofloops = 0;        [audioplayer play];   } }  @end 

edit: here code .h

#import <uikit/uikit.h> #import <coremotion/coremotion.h> #import <avfoundation/avfoundation.h> @interface lzdviewcontroller : uiviewcontroller //{avaudioplayer *audioplayer;}  @property (strong, nonatomic) cmmotionmanager *motionmanager; @property (strong, nonatomic) avaudioplayer *audioplayer;     @end 

i'm still not getting joy @ all, , apologise hand holding. white screen sound file never plays. have imported correct framework , can sound playing button, want play accelerometer. thanks

alright, first thing you're going want define 2 properties in either interface file, or in interface extension @ top of implementation file. these motion manager, , audio player.

@property (strong, nonatomic) cmmotionmanager *motionmanager; @property (strong, nonatomic) avaudioplayer *audioplayer; 

then can else done in viewdidload. code below sets new operation queue motion manager receive accelerometer events on, initializes audio player , motion manager, , starts getting accelerometer updates @ interval of 30 times per second.

finally, within accelerometers update block, use same logic build check acceleration data , if meets requirements continue on next condition.

**i have configured example not start playing sound again if player playing

- (void)viewdidload {     [super viewdidload];      nsoperationqueue *motionqueue = [nsoperationqueue new];      nsurl *url = [nsurl urlwithstring:[[nsbundle mainbundle] pathforresource:@"audiofile" oftype:@"m4a"]];     self.audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:nil];     [self.audioplayer preparetoplay];      self.motionmanager = [cmmotionmanager new];     [self.motionmanager setaccelerometerupdateinterval:1.0f/30.0f];      [self.motionmanager startaccelerometerupdatestoqueue:motionqueue withhandler:^(cmaccelerometerdata *accelerometerdata, nserror *error) {         if (fabsf(accelerometerdata.acceleration.x) > 1.8 || fabsf(accelerometerdata.acceleration.y) > 1.8 || fabsf(accelerometerdata.acceleration.z) > 1.8) {             nslog(@"%@",accelerometerdata);             if (self.audioplayer) {                 if (!self.audioplayer.isplaying) {                     [self.audioplayer play];                 }             }         }     }]; } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -