0

Thanks for the help.

This activates basic playback of referenced file:

NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

[sound play];

What's the proper way to terminate playback? No luck implementing the following:

NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

if([sound isPlaying])  {
    
    [sound stop];

Thanks for the assistance.

Paul
  • 189
  • 10

1 Answers1

0

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
DonMag
  • 69,424
  • 5
  • 50
  • 86