Re: Does NSSound not work in a Unix tool? - SOLUTION
Re: Does NSSound not work in a Unix tool? - SOLUTION
- Subject: Re: Does NSSound not work in a Unix tool? - SOLUTION
- From: Uli Zappe <email@hidden>
- Date: Fri, 22 Mar 2002 04:06:47 +0100
OK, here's what I've ended up with.
I decided it would be much easier and more appropriate for a Unix
tool to use Unix signals instead of a run loop. Still, one custom
object is needed as the delegate object for NSSound.
Following is the code for a complete little Unix tool that simply
plays the sound it is given as a parameter (only the name, without
path or file suffix). I will comment the code below.
#############################################################
## ExitController.h
#############################################################
#import <Cocoa/Cocoa.h>
@interface ExitController : NSObject
{
}
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool;
@end
#############################################################
## ExitController.m
#############################################################
#import "ExitController.h"
@implementation ExitController
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool
{
raise(SIGUSR1);
}
@end
#############################################################
## main.m
#############################################################
#import <Cocoa/Cocoa.h>
#import <unistd.h>
#import "ExitController.h"
inline void sigexit(int i)
{
exit(0);
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id controller=[[ExitController alloc] init];
NSSound *sound;
signal(SIGUSR1, sigexit);
if(argc>1) sound=[NSSound soundNamed:[NSString
stringWithCString:argv[1]]];
else sound=[NSSound soundNamed:@"Ping"];
[sound setDelegate:controller];
[sound play];
[pool release];
pause();
return 1;
}
#############################################################
The only purpose of the ExitController instance is to be NSSound's
delegate, and to send a User Defined 1 Unix signal to its own
process once NSSound is finished playing.
This signal is set to be caught by the sigexit() signal handling
function via the signal() function in main.m. sigexit() simply
exits the program. (It's not feasible to specify exit() directly as
the signal handling function in signal(), because exit() would get
SIGUSR1 (!=0) instead of 0 (meaning OK) as parameter.)
After NSSound starts playing the sound, pause() waits until the
SIGUSR1 signal is sent to the process. The return 1 should never be
reached.
Bye
Uli
________________________________________________________
Uli Zappe email@hidden
Lorscher Stra_e 5
http://www.ritual.org
D-60489 Frankfurt Fon: +49-700-ULIZAPPE
Germany Fax: +49-700-ZAPPEFAX
________________________________________________________
_______________________________________________
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.