I'm having trouble getting sounds to play reliably. When it happens, -[NSSound play] returns YES, but I hear nothing, and the sound:didFinishPlaying: delegate method is not called. The sound in question is an AIFF file with a duration of about half a second. It seems like whether it fails depends on how much other computation is going on after the play call, like maybe if it can't get enough CPU time right away, it gives up.

Here's my sound playing code.


@interface PlayDelegate : NSObject
- (id) init;
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying;
@end

@implementation PlayDelegate

- (id) init
{
        if ( (self == [super init]) != nil )
        {
        }
        return self;
}

- (void)sound:(NSSound *)sound
        didFinishPlaying:(BOOL)finishedPlaying
{
        [sound setDelegate: nil];
        [sound release];

        #if LOG_PLAY
                NSLog( @"Finished sound play %s",
                        finishedPlaying? "successfully" : "unsuccessfully" );
        #endif
}

@end

static PlayDelegate*    sPlayDelegate = nil;


void    PlayNamedSound( CFStringRef inSoundName )
{
        NSSound*        theSound = [NSSound soundNamed: (NSString*) inSoundName 
];
        
        if (theSound != nil)
        {
                [theSound retain];
                
                if (sPlayDelegate == nil)
                {
                        sPlayDelegate = [[PlayDelegate alloc] init];
                }
                
                [theSound setDelegate: sPlayDelegate];
                
                BOOL startedPlay = [theSound play];
                
        #if LOG_PLAY
                NSLog( @"Started sound play %s",
                        startedPlay? "successfully" : "unsuccessfully" );
        #endif
        }
        else
        {
                NSLog(@"Missing sound resource %...@.", (NSString*) inSoundName 
);
        }
}

--
  James W. Walker, Innoventive Software LLC
  <http://www.frameforge3d.com/>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to