Re: NSSound play often fails
Re: NSSound play often fails
- Subject: Re: NSSound play often fails
- From: Matt Neuburg <email@hidden>
- Date: Tue, 04 Aug 2009 08:37:42 -0700
- Thread-topic: NSSound play often fails
On Mon, 03 Aug 2009 11:28:04 -0700, James Walker <email@hidden>
said:
>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.
>
[snip]
> if ( (self == [super init]) != nil )
What on earth is *that* supposed to be????
>- (void)sound:(NSSound *)sound
> didFinishPlaying:(BOOL)finishedPlaying
>{
> [sound setDelegate: nil];
> [sound release];
>
>}
>
>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];
>
> }
> else
> {
> NSLog(@"Missing sound resource %@.", (NSString*) inSoundName );
> }
>}
It may be that some of your problems could be memory management problems.
You're grabbing control of a soundNamed: sound, retaining it, and then
releasing it in a different routine which, as you say yourself, may not ever
be called. This whole approach seems very weird. You should not mess with
the memory management of soundNamed: sounds, because there is only *one*
sound with a given name in your project, and you have no idea how the
framework is managing it. There is no reason to retain and release it, so my
first move here would be to get rid of your code that monkeys with the
management of the memory. Either load the sound from the file yourself with
NSSound alloc and initWithContentsOfFile, and release it when done, or just
use the auto-released sound provided for you by soundNamed:
Now, having said that, some trouble does arise if you try to play the sound
while it is already playing. To get around this, here's what I do in such
situations. When the app starts up, I make an array of NSSound objects, each
of which is an autoreleased *copy* of the sound provided by soundNamed:.
Then when it's time to play the sound, I search the array for a copy that
isn't playing, and tell it to play. Because the sound is a *copy*, it's mine
to manage, and because I collect the copies as the app starts up, I am
guaranteed that while the app is running, no sounds are loaded from disk (I
have loaded the already and the copies are just sitting in memory).
Having said all that, what I really do if I want good reliable play of sound
files is load the sound as a QTMovie. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden