On 2009 Aug 04, at 10:32, James Walker wrote:

Matt Neuburg wrote:

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...

Same here. I always just used the autoreleased sound, but it won't play more than once without a run loop.

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.

Same here.

Having said all that, what I really do if I want good reliable play of sound
files is load the sound as a QTMovie.

Okaaay... so NSSound is for when you want to play sound *unreliably*? :-)

Well, I'd have to learn QTMovie.  So, I tried James' solution first...

Anyway, as I mentioned elsewhere in this thread, I have switched to using SystemSoundPlay.

This System Sound API is quite interesting. In a Google search I found this:

   http://devworld.apple.com/technotes/tn2002/tn2102.html

In which...

"If your application makes use of alert sounds, using the System Sound APIs is a better approach than using some heavier weight methods to play
    these sound such as NSSound or QuickTime."

Great!!

So, I tried the sample code given in that tn2102, and indeed it succeeds in playing a sound correctly, repeatedly, without a run loop, whereas NSSound fails silently.

BUT unfortunately, I get warnings that all these functions are now depracated (see below).

So then I did another Google search and found that, probably in Mac OS 10.5 and later, the System Sound API is replaced by Audio Services in the Audio Toolbox framework.

Since these functions appear to be undocumented except for TN2102 and AudioServices.h, I put together a couple of demo programs so the next person won't have to hunt around so much. It appears that much of the System Sound API documentation in TN2102 applies to AudioServices also.

############## Program for Mac OS 10.3 or later
############## Depracation warnings compiled with 10.5 SDK
############## but still runs in 10.5

#import <Cocoa/Cocoa.h>

SystemSoundActionID gFunkSoundID = 0;

OSStatus CreateFunkActionID(void)
{
    const char *soundFilePath = "/System/Library/Sounds/Funk.aiff";
    FSRef soundFileRef;
    OSStatus err;

    err = FSPathMakeRef(soundFilePath, &soundFileRef, NULL);
    if (noErr == err) {
        err = SystemSoundGetActionID(&soundFileRef, &gFunkSoundID);
    }

    return err;
}

void MyCleanUpActionID(void)
{
    SystemSoundRemoveActionID(gFunkSoundID);
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
    CreateFunkActionID() ;
    AlertSoundPlayCustomSound(gFunkSoundID);
    usleep(2000000) ;
    AlertSoundPlayCustomSound(gFunkSoundID);
    usleep(2000000) ;
    AlertSoundPlayCustomSound(gFunkSoundID);
    usleep(2000000) ;
    MyCleanUpActionID() ;
    [pool release] ;
}


############## Program for Mac OS 10.5 or later
############## No depracation warnings with 10.5 SDK
############## Add System/Library/Frameworks/AudioToolbox.framework to project

#import <Cocoa/Cocoa.h>
#import <AudioToolbox/AudioServices.h>

SystemSoundID gFunkSoundID = 0;

OSStatus CreateFunkActionID(void)
{
CFURLRef soundFileUrl = (CFURLRef)[NSURL fileURLWithPath:@"/ System/Library/Sounds/Funk.aiff"];
    OSStatus err;

err = AudioServicesCreateSystemSoundID(soundFileUrl, &gFunkSoundID);

    return err;
}

void MyCleanUpActionID(void)
{
    AudioServicesDisposeSystemSoundID(gFunkSoundID);
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
    CreateFunkActionID() ;
    AudioServicesPlayAlertSound(gFunkSoundID);
    usleep(2000000) ;
    AudioServicesPlayAlertSound(gFunkSoundID);
    usleep(2000000) ;
    AudioServicesPlayAlertSound(gFunkSoundID);
    usleep(2000000) ;
    MyCleanUpActionID() ;
    [pool release] ;
}

_______________________________________________

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