Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:45:41+00:00 2026-06-09T02:45:41+00:00

I have created a simple app that triggers 3 different sounds based on the

  • 0

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!..

- (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),&audioRouteOverride);


    player.volume = 0.5;
    player.numberOfLoops = 0;
    player.delegate = self;
}

- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler 
{


    if (aceler.x > 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 > 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 > 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];
    };

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:45:43+00:00Added an answer on June 9, 2026 at 2:45 am

    Setting the accelerometer’s update frequency to a low value wont help here. Imagine the following situation:

    time:                          ------------------------>
    real world acceleration event: ___X_____X_____X_____X___ 
    acceleration update:           X_____X_____X_____X_____X
    sound output started:          _________________________
    

    This draft represents a user shaking the device with the same frequency as the accelerometer is updated. But the shake event occurs just in the middle between two accelerometer updates. As a result, the shake events won’t be registered and no sound is played.

    Consider a high accelerometer update frequency in contrast to the prior approach:

    real world acceleration event: ___X_____X_____X_____X___ 
    acceleration update:           XXXXXXXXXXXXXXXXXXXXXXXXX
    sound output started:          ___X_____X_____X_____X___
    

    Basically all real world events result in a sound play within this situation.

    The amended code is the following:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSLog(@"viewDidLoad");
    
        UIAccelerometer* accelerometer = [UIAccelerometer sharedAccelerometer];
        //[accelerometer setUpdateInterval: 25.0 / 10.0f];
        [accelerometer setUpdateInterval: 0.01f];
    
        [[AVAudioSession sharedInstance] setDelegate: self];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
        [accelerometer setDelegate:self];
    
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    
    
        //player.volume = 0.5;
        //player.numberOfLoops = 0;
        //player.delegate = self;
    
    }
    
    - (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler 
    {
    
    
        if ((aceler.x > ACC_THRESHOLD)&&((playerX == nil)||(playerX.isPlaying == NO))) {
            NSString *fileName = [NSString stringWithFormat:@"snare"];
            NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
            NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
            playerX = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
            NSLog(@"acceleration.x = %+.6f greater", aceler.x);
            playerX.delegate = self;
            [playerX play];
    
        }
    
        if ((aceler.y > ACC_THRESHOLD)&&((playerY == nil)||(playerY.isPlaying == NO))) {
            NSString *fileName = [NSString stringWithFormat:@"kick2"];
            NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
            NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
            playerY = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
            NSLog(@"acceleration.y = %+.6f greater", aceler.y);
            playerY.delegate = self;
            [playerY play];
    
        }
    
        if ((aceler.z > ACC_THRESHOLD)&&((playerZ== nil)||(playerZ.isPlaying == NO))) {
            NSString *fileName = [NSString stringWithFormat:@"hat"];
            NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
            NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
            playerZ = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
            NSLog(@"acceleration.z = %+.6f greater", aceler.z);
            playerZ.delegate = self;
            [playerZ play];
        }
    
        //else  {
        //    [player stop];
        //};
    
    }
    

    Please note, that multiple sound plays may be triggered by just one event as each dimension is evaluated separately now. A constant has been introduced by #define ACC_THRESHOLD 0.5f. A new sound play for one dimension is started only, after a previous play finished.

    After these general change of event handling you may start optimizations with a signal filter.

    Additionally you may use AVAudioPlayer’s delegate methods for more detailed sound handling:

    #pragma mark -
    #pragma mark AVAudioPlayerDelegate methods
    
    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    
        NSLog(@"audioPlayerDidFinishPlaying: %i", flag);
    
    }
    
    - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
    
        NSLog(@"audioPlayerDecodeErrorDidOccur: %@", error.description);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple app for android, that uses buttons to navigate different
I have created a simple map app that shows the local garage sales in
I am totally new to WPF, I have created a simple WPF app that
I have created a simple GAE app based on the default template. I want
I have created very simple app with persistence context (hibernate as provider) to read
I have created a simple web app/form using google app engine. the site is
I have created a small and simple android app. I tried installing it on
I've created a simple rails app that has a people and a notes model.
I am on windows 7 (64-bit) and I have created a simple app to
I have created a simple Facebook app and added it as a tab page.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.