Objective C:对 didFinishPLaying 委托方法的误解

发布于 2024-10-15 13:22:07 字数 1824 浏览 1 评论 0原文

我想熟悉 mac OSX 上的 Objective C/Interface Builder 编程。我通常使用声音,因此我尝试使用 NSSound 类编写一个简单的音频播放器。

我已经成功创建了一个基本应用程序,通过单击“导入器”按钮加载声音,然后通过单击“里拉”按钮播放声音。

这是我的问题:我想在窗口中有一个文本字段,在播放声音时显示“Ça joue Simone!...”,而在声音播放完毕时不显示任何内容。

当单击“里拉”按钮时,我想要的消息已成功显示,但即​​使声音播放完毕,它仍保留在那里。

这就是为什么我想我对如何使用 didFinishPLaying 委托方法有误解。

有人有提示吗?

//
//  ControleurLecteur.h
//  LecteurSon
//

#import <Cocoa/Cocoa.h>


@interface ControleurLecteur : NSObject {
    NSSound *son ;
    IBOutlet NSTextField *infoTextField ;
    IBOutlet NSTextField *cheminField ;
    IBOutlet NSTextField *durationField ;
}

-(IBAction)lireSon:(id)sender ;  
-(IBAction)importerSon:(id)sender ;  
-(IBAction)son:(NSSound *)son didFinishPlaying:(BOOL)bienjoue;  

@end

//
//  ControleurLecteur.m
//  LecteurSon
//


#import "ControleurLecteur.h"


@implementation ControleurLecteur

-(IBAction)importerSon:(id)sender { 
    NSString *Chemin = [[NSString alloc] initWithString:(NSString *)@"epno"] ;

    son = [[NSSound soundNamed:Chemin] retain];

    // Limiter l'affichage de la durée à 3 chiffres après la virgule
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setMaximumFractionDigits:3];
    [[durationField cell] setFormatter:numberFormatter];

    // Remplissage du champ de texte durée
    [durationField setFloatValue:[son duration]] ;  
}

-(IBAction)lireSon:(id)sender {

    BOOL bienjoue;
    bienjoue = [son play];
    bienjoue = TRUE;
    [infoTextField setStringValue:@"Ça joue Simone !..."];
    son:didFinishPlaying:bienjoue;
}


-(IBAction)son:(NSSound *)son didFinishPlaying:(BOOL)bienjoue {
    [infoTextField setStringValue:@""] ;
}


@end

I want to get familiar with the Objective C/Interface Builder programming on mac OSX. I'm usually working with sounds, so I'm trying to code a simple audio player, using the NSSound class.

I already managed to create a basic application that loads a sound by clicking an "Importer" button, and then play it by clicking a "Lire" button.

Here is my problem : I want to have a TextField in the window that displays "Ça joue Simone !..." when the sound is played, and nothing when the sound is finished playing.

When clicking the "Lire" button, the message I want is successfully displayed, but it stays there even after the sound is finished playing.

That's why I guess I have a misunderstanding on how to use the didFinishPLaying delegate method.

Does anybody have a hint ?

//
//  ControleurLecteur.h
//  LecteurSon
//

#import <Cocoa/Cocoa.h>


@interface ControleurLecteur : NSObject {
    NSSound *son ;
    IBOutlet NSTextField *infoTextField ;
    IBOutlet NSTextField *cheminField ;
    IBOutlet NSTextField *durationField ;
}

-(IBAction)lireSon:(id)sender ;  
-(IBAction)importerSon:(id)sender ;  
-(IBAction)son:(NSSound *)son didFinishPlaying:(BOOL)bienjoue;  

@end

//
//  ControleurLecteur.m
//  LecteurSon
//


#import "ControleurLecteur.h"


@implementation ControleurLecteur

-(IBAction)importerSon:(id)sender { 
    NSString *Chemin = [[NSString alloc] initWithString:(NSString *)@"epno"] ;

    son = [[NSSound soundNamed:Chemin] retain];

    // Limiter l'affichage de la durée à 3 chiffres après la virgule
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setMaximumFractionDigits:3];
    [[durationField cell] setFormatter:numberFormatter];

    // Remplissage du champ de texte durée
    [durationField setFloatValue:[son duration]] ;  
}

-(IBAction)lireSon:(id)sender {

    BOOL bienjoue;
    bienjoue = [son play];
    bienjoue = TRUE;
    [infoTextField setStringValue:@"Ça joue Simone !..."];
    son:didFinishPlaying:bienjoue;
}


-(IBAction)son:(NSSound *)son didFinishPlaying:(BOOL)bienjoue {
    [infoTextField setStringValue:@""] ;
}


@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

红玫瑰 2024-10-22 13:22:07

您必须在创建 son 的委托后对其进行设置:

son = [[NSSound soundNamed: Chemin] retain];
[son setDelegate: self];

否则,son 不知道将其委托消息发送到哪里。

您还必须注意保留代表消息的英文名称。如果你把它们翻译成法语,Objective-C 就无法理解它们(不过,参数的名称可以是任何东西。)而不是:

-(IBAction)son:(NSSound *)son didFinishPlaying:(BOOL)bienjoue

使用:

-(void)sound:(NSSound *)son didFinishPlaying:(BOOL)bienjoue

我认识的大多数开发人员都选择完全用英语编写代码,因为几乎所有编程语言使用基于英语的关键字、类名等,并将他们的母语与英语结合起来往往只会造成难以阅读的混乱。

You must set the delegate of son after you create it:

son = [[NSSound soundNamed: Chemin] retain];
[son setDelegate: self];

Otherwise, son has no idea where to send its delegate messages.

You must also take care to preserve the English names of delegate messages. If you translate them into French, they won't be understood by Objective-C (the names of arguments can be anything, however.) Instead of:

-(IBAction)son:(NSSound *)son didFinishPlaying:(BOOL)bienjoue

Use:

-(void)sound:(NSSound *)son didFinishPlaying:(BOOL)bienjoue

Most developers I know opt to code entirely in English, since almost all programming languages use English-based keywords, class names, etc., and combining their native languages with English tends to just make a difficult-to-read mess.

沫尐诺 2024-10-22 13:22:07

你忘记了代表:

@interface ControleurLecteur : NSObject <Son> {
}

You forgot delegate:

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