• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Playing a sound from a command-line app
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Playing a sound from a command-line app


  • Subject: Re: Playing a sound from a command-line app
  • From: Mark Dalrymple <email@hidden>
  • Date: Tue, 29 Apr 2003 14:37:04 -0400

Hi Neil,

> I'm guessing it's because the sound playing is asynchronous,
> and the app finishes before the sound plays.

You're pretty close. You actually need to have a runloop running, and the
runloop will ship the data off to the sound system. I tweaked your code,
and got this which plays the sound from the command line. There's a
dummy class there that exists just to be a delegate of the sound, to
know when it stops playing. When that happens, it sets a global
(per some advice at
http://cocoa.mamasam.com/COCOADEV/2002/08/1/42465.php
)
which then tells us to stop invoking the run loop.

NSRunLoop doesn't have a "stop" method, so there's no real convenient
way of stopping, short of letting the runloop run a little bit, then
seeing if our flag is set.

Cheers,
++Mark Dalrymple, email@hidden
http://borkware.com



/* compile wtih
cc -Wall -g -framework Foundation -framework Cocoa cmdsound.m
*/
#import <Cocoa/Cocoa.h>

BOOL g_keepRunning = YES;


// an object to be the delegate of the sound, so we can be
// notified when it stops
@interface Stopper : NSObject
{
}

- (void) sound: (NSSound *) sound didFinishPlaying: (BOOL) aBool;

@end // Stopper


@implementation Stopper

- (void) sound: (NSSound *) sound didFinishPlaying: (BOOL) aBool
{
g_keepRunning = NO;

} // didFinishPlaying

@end // Stopper



int main (int argc, char *argv[])
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];

NSSound *sound;
sound = [[NSSound alloc]
initWithContentsOfFile: @"/System/Library/Sounds/Glass.aiff"
byReference: YES];

// make a delegate object
Stopper *stopper;
stopper = [[[Stopper alloc] init] autorelease];

[sound setDelegate: stopper];

[sound play];

// need a runloop to actually get the sound bytes out to the
// speaker.
NSRunLoop *runLoop;
runLoop = [NSRunLoop currentRunLoop];

// basically poll until we're done. unfortunately NSRunLoop doesn't
// have a "stop" method.
while (g_keepRunning) {
[runLoop runUntilDate:
[NSDate dateWithTimeIntervalSinceNow: 1.0]];
}

[pool release];

return (0);

} // main
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Playing a sound from a command-line app (From: Neil Rhodes <email@hidden>)

  • Prev by Date: Re: Newbie Build Error
  • Next by Date: RE: how to get OS name ?
  • Previous by thread: Playing a sound from a command-line app
  • Next by thread: Include arbitrary dylib in project
  • Index(es):
    • Date
    • Thread