如何让 NSSound 在用户定义的类中播放声音?

发布于 2024-10-31 05:50:35 字数 1163 浏览 1 评论 0原文

这个问题来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-11-07 05:50:35

如前所述,解决方案是在 NSSound 后面添加 sleep(...); 语句。以下是对 SoundMaker.m 的有效更改:

#import "SoundMaker.h"

@implementation SoundMaker
+(void) makeSound
{
    NSLog(@"This is SoundMaker.");
    NSBeep(); // Works fine.
    sleep(1);
    [[NSSound soundNamed:@"Frog"] play]; // Works fine.
    sleep(1); // This added statement allows the preceding "play" message to work.
}
@end

So as noted, the solution is to add a sleep(...); statement following NSSound. Here is the change to SoundMaker.m that works:

#import "SoundMaker.h"

@implementation SoundMaker
+(void) makeSound
{
    NSLog(@"This is SoundMaker.");
    NSBeep(); // Works fine.
    sleep(1);
    [[NSSound soundNamed:@"Frog"] play]; // Works fine.
    sleep(1); // This added statement allows the preceding "play" message to work.
}
@end
庆幸我还是我 2024-11-07 05:50:35

在 Xcode 中,如果设置断点,它会被击中吗?

我自己最近才开始,所以 +(void) 对我来说看起来很奇怪;我正在使用的书只教

- (return_variable)methodname

[编辑]

我刚刚测试了它,添加

- (void)playFrog;

到我的界面。

函数

- (void)playFrog
{
[[NSSound soundName:@"Frog"] play];
}

然后,我实现了从应用程序中其他地方调用它的

,例如[self playFrog];

它起作用了。

[编辑2]

再次阅读您的代码后,我注意到您没有分配/初始化您的SoundMaker...

尝试

SoundMaker *mySoundMaker = [[SoundMaker alloc] init];
[mySoundMaker makeSound];
[mySoundMaker release];

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

- (return_variable)methodname

[Edit]

I just tested it, added

- (void)playFrog;

To my interface.

I then implemented the function

- (void)playFrog
{
[[NSSound soundName:@"Frog"] play];
}

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

SoundMaker *mySoundMaker = [[SoundMaker alloc] init];
[mySoundMaker makeSound];
[mySoundMaker release];
不及他 2024-11-07 05:50:35

大多数其他答案虽然还可以,但让它看起来比需要的更复杂。无论您想要在何处产生声音,您所需要的只是这个语句(在 Objective-C 中):

[[NSSound soundNamed:@"Hero"] play];

您可以在系统偏好设置的声音窗口中找到并测试各种声音名称。

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:

[[NSSound soundNamed:@"Hero"] play];

You can find and test the various sound names in the Sound window of System Preferences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文