This question is from an Objective-C newbie, so please bear with me. I'm trying to make sounds in my classes and have been successful using NSBeep() but not NSSound. Here is an example. Notice that NSBeep(); and [[NSSound soundNamed:@"Frog"] play]; work fine in the "main.m" program, but only NSBeep(); works in the SoundMaker class. Any help in learning how to get NSSound to work is much appreciated.
main.m:
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "SoundMaker.h"
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"This is the main program.");
NSBeep(); // Works fine.
sleep(1);
[[NSSound soundNamed:@"Frog"] play]; // Works fine.
sleep(1);
[SoundMaker makeSound]; // Only NSBeep() works.
[pool drain];
return 0;
}
SoundMaker.h:
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface SoundMaker : NSObject
+(void) makeSound;
@end
SoundMaker.m:
#import "SoundMaker.h"
@implementation SoundMaker
+(void) makeSound
{
NSLog(@"This is SoundMaker.");
NSBeep(); // Works fine.
sleep(1);
[[NSSound soundNamed:@"Frog"] play]; // Doesn't work.
}
@end
So as noted, the solution is to add a
sleep(...);statement followingNSSound. Here is the change to SoundMaker.m that works: