views:

107

answers:

1

I have an array that creates several different sound file names in a specific order. I have successfully created the array but I am not sure how to call the value I got from the array as the file URL or how I implement it into AudioServicesPlaySystemSoundID.

This is the code I have so far:

       - (void)viewDidLoad {
            NSArray *sounds = [[NSArray alloc] initWithObjects: @"0.wav", @"1.wav", @"2.wav, nil];
            NSUInteger currentSound = 0;
            soundArray = sounds
            [super viewDidLoad];
        }

        - (void) playFailSound {
            currentSound++;
            if (currentSound >= [sounds count]) {
             currentSound = 0;
            }
            [self playSoundWithFilename:[sounds objectAtIndex:currentSound]];
        }

I'm also not sure what I need to declare in the header file for this to work and how do I store the value of the array?

A: 

Are you asking how to call playFailSound: ? Or are you asking how to declare the sounds array in your header file to make it an instance variable?

The first problem you have is that you're using a different variable name for the array in the two methods. in viewDidLoad you're using soundArray, and in playFailSound you're using sounds.

In your header file, you'll need to declare an array as an instance variable:

#import <UIKit/UIKit.h>

@interface MyObject : NSObject {
    NSArray *_sounds;    //declare the variables
    NSInteger _currentSound;  //this doesn't need to be unsigned, does it?

}

@property(nonatomic, retain) NSArray *sounds;  //property
@property(value) NSInteger currentSound;  //property


//method declarations 
- (void) playFailSound;    
- (void) playSoundWithFilename:(NSString *)fileName;

@end

You'll notice I used an underscore in the name of the variable, but didn't in the property. This way you won't accidently use the variable when you mean to use the property.

In your implementation file you'll need the following:

#import "MyObject.h"

@implementation MyObject

//synthesize the getters and setters,  tell it what iVar to use
@synthesize sounds=_sounds, currentSound=_currentSound; 

   - (void)viewDidLoad {
        NSArray *tempSounds = [[NSArray alloc] initWithObjects: @"0.wav", 
                                                            @"1.wav", 
                                                            @"2.wav, nil];
        self.currentSound = 0; //use the setter
        self.sounds = tempSounds; //use the setter 
        [tempSounds release]; //we don't need this anymore, get rid of the memory leak
        [super viewDidLoad];
    }

    - (void) playFailSound {
        self.currentSound=self.currentSound++; //use the getters and setters
        if (self.currentSound >= [self.sounds count]) {
            self.currentSound = 0;
        }
        [self playSoundWithFilename:[self.sounds objectAtIndex:self.currentSound]];
    }

    - (void) playSoundWithFilename:(NSString *)filename {
        //you fill this in
    }
@end

Now all you need to do is call playFailSound from somewhere, and fill in the part that actually plays the sound.

Essentially, for a two methods to refer to the same variable that isn't passed between them, it needs to be a instance variable.

This is pretty basic stuff, so if you don't get what I'm explaining here, I'd suggest re-reading some of Apple's introductory materials.

Nathaniel Martin
Don't forget to dealloc.
Jonathan Sterling
Of course, thanks!
Nathaniel Martin