views:

250

answers:

1

Hi All,

I am trying to play two .wav files in my appllication, but i am only able to play the first wav file , the second file is not at all playing.

I have added the AudioToolbox framework. In my .h file i have included the Audiotoolbox.h file and has the following declarations

//Beep Sound
SystemSoundID   soundFileObject;
SystemSoundID   recycleFileObject;

In my .m file i am registering both the wav files as following

CFURLRef soundFileURLRef = CFBundleCopyResourceURL (CFBundleGetMainBundle (),CFSTR ("Beep"),CFSTR ("wav"),NULL );
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject );

CFURLRef soundFileURLRef2 = CFBundleCopyResourceURL (CFBundleGetMainBundle (),CFSTR ("Recycle"),CFSTR ("wav"),NULL );
AudioServicesCreateSystemSoundID (soundFileURLRef2,&recycleFileObject );

And i have written two methods for playing each file individually.

-(void)playBeepSound
{
    NSLog(@"Beep sound called : %d", soundFileObject);
    AudioServicesPlaySystemSound (soundFileObject);
}


-(void)playRecycleSound
{
    NSLog(@"Recycle sound called : %d", recycleFileObject);
    AudioServicesPlaySystemSound (recycleFileObject);
}

But i am only able to play the Beep sound , but not the second sound wav file.

What is the error ???

Thanks

A: 

If I were you I would wrap the Audio Toolbox code in a class so that you can factor out common code and play sounds with more comfort (example code). This could also solve your problem, as it seems to be a small typo somewhere – the sample code you gave us looks fine. (Did you try to log everything? No nulls, everything gets called as expected? Are you trying to play both sounds at once, or is there a delay between them? Does it matter which sound runs first?)

zoul
When the sound finishes playing, we need to deallocate that sound object - how do we know when the sound stops? (using your wrapper)
Jonny
System sound services are usually used for short UI effects. This means that they are meant to be recycled: you’ll have just one instance of a certain sample playing over and over without deallocating. It can go out of memory when a view controller gets deallocated, for example.
zoul