开发者

Objective C : Misunderstanding with the didFinishPLaying delegate method

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 开发者_JS百科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


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.


You forgot delegate:

@interface ControleurLecteur : NSObject <Son> {
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜