这个问题来自一个Objective-C新手,所以请多多包涵。我正在尝试在我的课程中发出声音,并且使用 NSBeep() 但不是 NSSound 成功。这是一个例子。请注意,NSBeep();
在[[NSSound soundNamed:@"Frog"] play];
“main.m”程序中工作正常,但仅NSBeep();
在 SoundMaker 类中工作。非常感谢学习如何让 NSSound 工作的任何帮助。
主.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