Re: Question & one Answer for NSButton -- Ok. Answer for pause issue. ugly trick, but works!
Re: Question & one Answer for NSButton -- Ok. Answer for pause issue. ugly trick, but works!
- Subject: Re: Question & one Answer for NSButton -- Ok. Answer for pause issue. ugly trick, but works!
- From: tyler <email@hidden>
- Date: Mon, 6 Aug 2001 22:50:33 -0700
Ok. Ok. Here I go and answered my question about how to avoid the
pause. Here's the trick in case anyone out there needs it:
Just play and then stop the sound and it'll not actually play, but will
do whatever initialization (or whatever) is needed. A more elegant fix
would be good to know, but in the mean time this appears to work:
- (void) awakeFromNib
{
NSString * path = [[NSBundle mainBundle] pathForSoundResource:
@"click.aiff"];
clickSnd = [[NSSound alloc] initWithContentsOfFile: path
byReference: NO] // byRef ??
[clickSnd play]; // get it to do whatever initialization causes
pause
[clickSnd stop]; // but we don't want to actually hear the sound
right now, thanks...
}
peace,
tyler
On Monday, August 6, 2001, at 10:42 PM, tyler wrote:
Hmm. So the one aspect of this "let the target handle it" fix, is that
there is a significant pause the first time you click on the button
(actually loading the sound? or perhaps initializing the NSSound object
somehow?). It's quite fast after that one first time, but the pause
(heck, it's almost a HANG it's so long) is a bummer.
Any ideas to get NSSound to preload or pre-initialize or whatever so it
doesn't hang like that?
Thanks!
peace,
tyler
On Monday, August 6, 2001, at 10:36 PM, tyler wrote:
OK. Here's an answer - just make my target of the button handle the
sound... seems easier, but a bit less elegant than having the button
do it.
anybody got a better fix?
tyler
//
-----------------------------------------------------------------------------------
Ok. So here's the implementation that has the NSButton target handle
the sound
in case anyone might find it useful. Shockingly simple (let's hear it
for Cocoa!).
// In my target object (controller for window) I have a method that is
the action method of the NSButton:
- (IBAction)doSwitch:(id)sender
{
[self playSwitchSound]; // play sound each time it's hit.
// ... do other stuff here in response to button being clicked upon.
}
// A simple initialization in awakeFromNib to pre-load the sound:
- (void) awakeFromNib
{
NSString * path = [[NSBundle mainBundle] pathForSoundResource:
@"click.aiff"];
clickSnd = [[NSSound alloc] initWithContentsOfFile: path
byReference: YES]; // byRef ??
// anyone know what the "byReference" param is for/does?
}
// and the method to actually play the sound
- (void) playSwitchSound
{
BOOL b;
if ( clickSnd )
b = [clickSnd play];
// Anyone know what the boolean return means?
}