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 7752369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:46:56+00:00 2026-06-01T11:46:56+00:00

Right now I have a single sound playing from one action, button press, accelerometer,

  • 0

Right now I have a single sound playing from one action, button press, accelerometer, etc..

I would like to know how I can cycle through several sounds (I’m using 3 sounds for my project) from a single action that the user initiates.

I am currently using the code shown below and it serves it’s purpose for playing a single sound for each user action. I have not used NSArray in a project before, so if your including it please include any details.

NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/Jet.wav", [[NSBundle mainBundle] resourcePath]]];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = 0;

        if (audioPlayer == nil)
            NSLog(@"%@", [error description]);
        else 
            [audioPlayer play];
  • 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-01T11:46:58+00:00Added an answer on June 1, 2026 at 11:46 am

    If you are only using 3 sounds then you can get away with just using a C array of NSString‘s but if you need a dynamic amount of sounds then you should change to using an NSArray

    // .m
    NSString *MySounds[3] = {
        @"sound1.wav",
        @"sound2.wav",
        @"sound3.wav",
    };
    
    @implementation ...
    

    Then in your method you need to add a little extra logic

    - (void)playSound;
    {
        NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], [self nextSoundName]];
    
        NSURL *url = [NSURL fileURLWithPath:path];
    
        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = 0;
    
        if (audioPlayer == nil) {
            NSLog(@"%@", [error description]);
        } else {
            [audioPlayer play];
        }
    }
    
    - (NSString *)nextSoundName;
    {
        static NSInteger currentIndex = -1;
    
        if (++currentIndex > 2) {
            currentIndex = 0;
        }
    
        return MySounds[currentIndex];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I right now have a function the drags an element from a
Okay right now I have too include the code below on every single page
right now, we have a single server with a cronjob tab that sends out
Right now I have simple form with a single input. I'm trying to use
Right now I have google map code that will set a single marker on
Im new to webforms. Right now I have a DetailsView of a single client...
right now I have min, max value from table1 that defines the range of
I have a single text box form on my home page (/). Right now
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now I have an SSIS package that runs every morning and gives me

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.