views:

27

answers:

1

hello in my application i use the code below to handle and play sounds, I want to know if i need to release any things or nothing to do.

the code :

 -(void) playNote: (NSString*) note type: (NSString*) type
{

CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
    // Get the URL to the sound file to play
    soundFileURLRef  =  CFBundleCopyResourceURL (mainBundle,
            (CFStringRef)note,
            (CFStringRef)type,
            NULL);

    // Create a system sound object representing the sound file
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

    AudioServicesPlaySystemSound (soundFileObject);
}

Thanks ,

+1  A: 

You would need to call

AudioServicesDisposeSystemSoundID

to clean up when you are done with the sound.

In your header

@interface xyz
{
  SystemSoundID soundFileObject;
};

in your .m file, create an init method (if you dont have one):

-(id)init
{
   if ( (self = [super init]) )
   {
     AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);
   }
   return self;
}

in your dealloc

-(void)dealloc
{
   AudioServiceDisposeSystemSoundID(soundFileObject);
   [super dealloc];
}

you should declare the soundFileURLRef also as a ivar

Anders K.
hello thanks for answer just i write this code without passing any parameter ?
Bobj-C
you need to pass what CreateSystemSoundID returned i.e. the second param returns soundsFileObject, this you need to pass. Probably better to create it elsewhere and put the Dispose in the dealloc.
Anders K.
ok if i write AudioServicesDisposeSystemSoundID (soundFileObject) the compiler make error for soundFileObject undeclared first use this is right because soundFileObject is a local variable what is the solution now ?
Bobj-C
you need to declare it as a member in your class, then set it up in the init method and destroy it in the dealloc.
Anders K.
either that or do it all in your method although I suspect it would be a waste since you may want to play your sound several times.
Anders K.
i tried to declare it as a member variable but i failed can u help me please ? thanks
Bobj-C
ok see edited answer
Anders K.
thanks again but the same error appear undeclared first use (soundFileObject) also I want tell how can i used soudFileURLRef before i assign a value for it ?
Bobj-C