views:

399

answers:

5

I'm very new at Objective C and developing for the iPhone so I apologize if this is an easy answer I've tried searching Google for going on 3 days now and bought iPhone Development for Dummies and no luck on something like this.

Essentially what I would like to do is play a sound when a button is pressed and then I would like to add 1 to the value of the audio file. So for example

//Play audio file (y.wav)   ---y being the variable I would like to set.
if (y > 13) {               ---there are a total of 14 sounds starting with file 0.wav going to 13.wav.
   y = 0;                   ---so if the value is greater than 13 I want to start the cycle over with y equaling 0.
} else {
   y++;                     ---if y is not greater than 13 I want to add one to it so it plays the next sound file next time.
}

I do have a some history with JavaScript so this example reflects JavaScript more than Objective-C. The app is similar to iFart but before you get on my case about not making another farting/burping application that's not actually what I am making it is just a similar concept for this portion of my app. So if someone could help me learn how translate this into Objective-C or maybe an entirely different method of reaching my goal I would greatly appreciate it.

Thanks, Joey

A: 

You'll want to do something like this:

for ( NSUInteger i = 0; i < 14; i++ ) {
    NSString *filename = [ NSString stringWithFormat:@"%d.wav", i ];
    // Code to play the sound
}

There are other ways to do this, but do check out the developer documentation for NSString's +stringWithFormat: method.

Jeff Kelley
+1  A: 
- (void) playMySound
{
    static int soundIndex = 0;
    const int soundCount = 14;

    // create sound name from base string and wound index, as Jeff shows above

    // play the sound

    if (++soundIndex >= soundCount)
    {
        soundIndex = 0;
    }

    // or just do it in one line, viz
    //soundIndex = (soundIndex + 1) % soundCount;

}
Amagrammer
A: 

Whenever you want to cycle some integer value, it's good pratice to just use the modulo operator during the increment. The general pattern is

counter = (counter + 1) % maxValue;

This will ensure counter is always between 0 and (maxValue - 1), which is ideal for cycling through an array with maxValue elements.

I'd also recommend being flexible about the actual number of sounds you can play and the file names. Thus, you can just store the names of your sounds in an NSAarry.

in your class, you should have two fields. The array that holds the names of the sounds and an integer that holds the number of the current sound to play:

NSUinteger currentSound;
NSMutableArray *soundArray;

Initialize your array. For example in viewDidLoad...

currentSound = 0;
// get the base URL of the app bundle
NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
NSMutableArray *sounds = [[NSMutableArray alloc] init];  
// remember to release the array and the player objects on dealloc.
// add sounds...
NSError *p_error = nil;
[sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
    [NSURL URLWithString: @"1.wav" relativeToURL: baseURL] error: &p_error]];
// check error an initialize other sounds...

... now in the action that is triggered when the button is pressed you do...

AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
currentSound = (currentSound + 1) % [soundArray count];
[player play];

The modulo (%) operator will work so that once currentSound equals the count of objects in the array, it will flip back to 0.

VoidPointer
A: 

Okay so this is what I've got.

- (void)viewDidLoad {
        currentSound = 0;
        CFBundleRef mainBundle;
        mainBundle = CFBundleGetMainBundle ();
        NSURL *baseURL =[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
        NSMutableArray *sounds = [[NSMutableArray alloc] init];
        NSError *p_error = nil;
        [sounds addObject: [[AVAudioPlayer alloc] initWithContentsOfURL:
        [NSURL URLWithString: @"0.wav" relativeToURL: baseURL] error: &p_error]];


        soundArray = sounds;
        [super viewDidLoad];
    }

    - (IBAction)playFailSound {
        AVAudioPlayer *player = [soundArray objectAtIndex: currentSound];
        currentSound = (currentSound + 1) % [soundArray count];
        [player play];

However, I'm still not sure how to add my own sound files. And do I need to declare AVAudioPlayer in the header file? If so how would I go about that?

Again I apologize for making things overly complicated but I'm trying and don't tend to get along with the Apple documentations.

infiniteloop91
A: 

I'm interested in doing the same thing. Were you successful?

jim