Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you make the iphone acceleromator trigger sound more effectively using avaudioplayer?
    primarykey
    data
    text
    <p>I have created a simple app that triggers 3 different sounds based on the x, y, z axis of the accelorometer, like an instrument. At the moment, if I set the frequency update interval of the accelometer too low, it plays the sound to much, and if I set it too high it it isn't responsive enough. I am a complete beginner to objective c and iphone development, can you tell by the code!..</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; // Do any additional UIAccelerometer* accelerometer = [UIAccelerometer sharedAccelerometer]; [accelerometer setUpdateInterval: 25.0 / 10.0f]; [[AVAudioSession sharedInstance] setDelegate: self]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil]; [accelerometer setDelegate:self]; UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&amp;audioRouteOverride); player.volume = 0.5; player.numberOfLoops = 0; player.delegate = self; } - (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler { if (aceler.x &gt; 0.5) { NSString *fileName = [NSString stringWithFormat:@"snare"]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; NSLog(@"acceleration.x = %+.6f greater", aceler.x); [player play]; } else if (aceler.y &gt; 0.5) { NSString *fileName = [NSString stringWithFormat:@"kick2"]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; NSLog(@"acceleration.y = %+.6f greater", aceler.y); [player play]; } else if (aceler.z &gt; 0.5) { NSString *fileName = [NSString stringWithFormat:@"hat"]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; NSLog(@"acceleration.y = %+.6f greater", aceler.z); [player play]; } else { [player stop]; }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload