如何让 NSSound 在用户定义的类中播放声音?
这个问题来自 Objective-C 新手,请耐心等待。我试图在我的课堂上发出声音,并且使用 NSBeep() 取得了成功,但没有成功使用 NSSound。这是一个例子。请注意, NSBeep();
和 [[NSSound soundNamed:@"Frog"] play];
在“main.m”程序中工作正常,但只有 NSBeep();
在 SoundMaker 类中工作。非常感谢任何帮助学习如何让 NSSound 工作的帮助。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如前所述,解决方案是在
NSSound
后面添加sleep(...);
语句。以下是对 SoundMaker.m 的有效更改:So as noted, the solution is to add a
sleep(...);
statement followingNSSound
. Here is the change to SoundMaker.m that works:在 Xcode 中,如果设置断点,它会被击中吗?
我自己最近才开始,所以 +(void) 对我来说看起来很奇怪;我正在使用的书只教
[编辑]
我刚刚测试了它,添加
到我的界面。
函数
然后,我实现了从应用程序中其他地方调用它的
,例如[
self playFrog];
,它起作用了。
[编辑2]
再次阅读您的代码后,我注意到您没有分配/初始化您的SoundMaker...
尝试
In Xcode, if you set a breakpoint, does it get hit?
I have started only recently myself, so the +(void) looks weird to me; the book I am using only teaches
[Edit]
I just tested it, added
To my interface.
I then implemented the function
I called it from somewhere else in my application like
[
self playFrog];
That worked.
[Edit 2]
After reading your code again, I noticed you didn't alloc/init your SoundMaker...
Try
大多数其他答案虽然还可以,但让它看起来比需要的更复杂。无论您想要在何处产生声音,您所需要的只是这个语句(在 Objective-C 中):
您可以在系统偏好设置的声音窗口中找到并测试各种声音名称。
Most of the other answers, while okay, make it look more complicated than it needs to be. All you need is this statement (in Objective-C) wherever you want the sound to be produced:
You can find and test the various sound names in the Sound window of System Preferences.