From the couple code snippets you included in your question, it looks like you are doing this:
// create new sound object
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];
// start it playing
[sound play];
and then you create another new sound object:
// create new sound object
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];
// this cannot be true - you just created the sound object
if([sound isPlaying]) {
[sound stop];
}
Try this very simple example (just add Play and Stop buttons to a view controller and connect them):
#import "ViewController.h"
@interface ViewController() {
NSSound *sound;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// instantiate sound object with file from bundle
sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1162" ofType:@"aiff"] byReference:NO];
}
- (IBAction)playClicked:(id)sender {
if (sound) {
[sound play];
}
}
- (IBAction)stopClicked:(id)sender {
if (sound && [sound isPlaying]) {
[sound stop];
}
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
@end