Re: NSSound play often fails
Re: NSSound play often fails
- Subject: Re: NSSound play often fails
- From: James Walker <email@hidden>
- Date: Tue, 04 Aug 2009 10:32:20 -0700
Matt Neuburg wrote:
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:
Originally my code was simpler (less monkeying), but when I noticed that
the sound was not always playing, I started grasping at straws. The
NSSound reference says "You should retain NSSound objects before
initiating playback", so I thought maybe I should do that. If
soundNamed: returns an autoreleased sound, what if the sound gets
released while it's playing?
Now, having said that, some trouble does arise if you try to play the sound
while it is already playing.
I'm sure that wasn't my problem. It's a short sound, and not being
played rapid-fire.
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.
Okaaay... so NSSound is for when you want to play sound *unreliably*? :-)
Anyway, as I mentioned elsewhere in this thread, I have switched to
using SystemSoundPlay.
--
James W. Walker, Innoventive Software LLC
<http://www.frameforge3d.com/>
_______________________________________________
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