Objective C:对 didFinishPLaying 委托方法的误解
我想熟悉 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在创建
son
的委托后对其进行设置:否则,
son
不知道将其委托消息发送到哪里。您还必须注意保留代表消息的英文名称。如果你把它们翻译成法语,Objective-C 就无法理解它们(不过,参数的名称可以是任何东西。)而不是:
使用:
我认识的大多数开发人员都选择完全用英语编写代码,因为几乎所有编程语言使用基于英语的关键字、类名等,并将他们的母语与英语结合起来往往只会造成难以阅读的混乱。
You must set the delegate of
son
after you create it: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:
Use:
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.
你忘记了代表:
You forgot delegate: