How do I call an array value and use it to play a sound file?
صفحه اصلي

How do I call an array value and use it to play a sound file?



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?


Generating Random Numbers in Objective C for iPhone SDK

1:

Obj-C… “Incompatible types in initialization” error
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?. Javascript sort with function not working on iPhone The first problem you have is this you're using a different variable name for the array in the two methods. SubViewTwoController undeclared (first use in this function) (obj-c)in viewDidLoad you're using soundArray, and in playFailSound you're using sounds.. UITableView+UINavigationBar+UITabbar in windows based application template using interface Builder? In your header file, you'll need to declare an array as an instance variable:. Core Data: Quickest way to delete all instances of an entity
#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, although didn't in the property. Is there a portable OpenGL UI Library (Buttons, Lists, Windows, Dropdown etc)?This way you won't accidently use the variable when you mean to use the property.. Downloading images from a server iPhone SDK In your implementation file you'll need the following:.
#import "MyObject.h"  @implementation MyObject  //synthesize the receive ters 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]; //i don't need this anymore, receive  rid of the memory leak         [super viewDidLoad];     }      - (void) playFailSound {         self.currentSound=self.currentSound++; //use the receive ters 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 did is call playFailSound from any where, and fill in the part this actually plays the sound.. Essentially, for a two methods to refer to the same variable this isn't passed between them, it needs to be a instance variable.. This is pretty basic stuff, so if you don't receive what I'm explaining here, I'd suggest re-reading any of Apple's introductory materials..


90 out of 100 based on 35 user ratings 350 reviews

*