Playing short sound on timer end? - iphone

In my app I have a timer that counts down, and when it stops an image is displayed and a sound is played.
The sound is loaded like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"scream" ofType:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
And Played like this on timer end:
AudioServicesPlaySystemSound(soundID);
My problem is that the sound doesn't get played on time. Even though the sound instance is called before the image, the image appears before the sound. This only occurs the first time the sound is played. The sound is loaded in the viewDidLoad method.
Is there a better way to do it, or what am I doing wrong?
Thanks...

Maybe the sound is delayed while the speaker is powered up (for the first sound). If so, maybe you can work around it by playing a different, unnoticeable sound earlier so that the speaker is ready to go when you request the "real" sound.

Related

SystemSoundID Sometimes Changing With Device Volume

I am playing sound effects in my game using the SystemSoundID class, with some odd results. A few of my sounds, including landed1 below, change volume according to the volume buttons, but others, like deathsound always play at full volume. Below is all code related to the two sounds.
SystemSoundID deathsound;
SystemSoundID landed1;
...
NSURL *soundURL;
soundURL = [[NSBundle mainBundle] URLForResource:#"SMPKLanded1" withExtension:#"mp3"];
AudioServicesCreateSystemSoundID ((__bridge CFURLRef) soundURL, &landed1);
soundURL = [[NSBundle mainBundle] URLForResource:#"SMPKDeathSound" withExtension:#"mp3"];
AudioServicesCreateSystemSoundID ((__bridge CFURLRef) soundURL, &deathsound);
...
AudioServicesPlaySystemSound(landed1);
...
AudioServicesPlaySystemSound(deathsound);
Why would the two act so differently, if the code and filetype are essentially the same? It makes no sense!
Well, I finally figured it out, after inordinate amounts of time driving myself crazy. Some of them are being played at the same time as an AVAudioPlayer, which for some reason made them conform to the volume buttons, and some weren't. My obvious next step would be to make a silent AVAudioPlayer, which I have now done, and the program works as I would expect.
As a follow up question, does anybody have any clue why having an AVAudioPlayer affects SystemSoundIDs?

AudioServicesPlaySystemSound vibration works but not sound

AudioServicesPlaySystemSound doesn't do anything but AudioServicesPlayAlertSound makes the iPhone vibrate.
AudioServicesCreateSystemSoundID returns success. I'm developing on iPhone3G running iOS4.
Any ideas?
Peter
=====
Here's how I create the sound:
NSString *sndPath = [[NSBundle mainBundle] pathForResource:#"first_touch" ofType:#"wav" inDirectory:#"/"];
CFURLRef sndURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndPath];
int e = AudioServicesCreateSystemSoundID(sndURL, &_firstTouch); // TODO: call 'AudioServicesDisposeSystemSoundID'
And that's who I play it:
AudioServicesPlayAlertSound(_firstTouch);
Showing some code helps.
At a guess, you're creating a sound, playing the sound, and then immediately deleting the sound. This doesn't work; deleting the sound causes it to stop playing.
I'm not deleting the sound. I added the source code.
After I rebooted both my machine and the iPhone, it started working on the simulator but not on the iPhone.
It worked once on the iPhone. I'm testing with a new project with just the create and play sound in it.

Can't hear System Sound in simulator or on device

I can't seem to get the AudioServices sounds to play, either on the device or in the simulator. Here is my code:
NSString *sndPath = [[NSBundle mainBundle] pathForResource:#"click" ofType:#"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:sndPath], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
I have checked my system prefs, and yes I do have "Play user interface sound effects" ticked on... What might I be doing wrong?
Thanks!
EDIT: I'm attempting to create an audio click as the user moves touches across the device, so I need it to rapidly play the same sound. Should I use AVAudioPlayer instead?
Don't dispose the sound until after it finishes playing. You can do something like this:
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, (AudioServicesSystemSoundCompletionProc)AudioServicesDisposeSystemSoundID, NULL);
Slightly icky, since we're assuming that it doesn't use the Pascal calling convention (i.e. AudioServicesDisposeSystemSoundID() can handle the extra NULL argument).

Stop sound in iPhone

SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:#"static" ofType:#"caf"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
I have implemented above code in my application.
It works perfectly, but the problem is I don't know how to stop it.
i.e Sound is of 10 sec.
Before 10 sec. if user taps button, sound should be stopped? How can I do so?
Try AudioServicesDisposeSystemSoundID, it stops sounds immediately. You'll need to recreate the sound if you intend to use it again.
Use the following instruction for disposes of a system sound object and associated resources:
AudioServicesDisposeSystemSoundID(soundID);
I would suggest using the AVFoundation framework. Here is an amazing tutorial on how to play, pause, and stop sound using the AVAudioPlayer.

How do I record and play back sound on the iPhone?

In my app I've got certain buttons which use the following code:
SystemSoundID beep;
CFStringRef beepPath = (CFStringRef) [NSString stringWithFormat: #"%#/SoundFile.aif", [[NSBundle mainBundle] bundlePath]];
AudioServicesCreateSystemSoundID (CFURLCreateWithFileSystemPath (NULL, beepPath, kCFURLPOSIXPathStyle, false), &beep);
CFRelease(beepPath);
AudioServicesPlaySystemSound (beep);
I would like the user to start recording and be able to record any sounds made by the app and then have the ability to play the recorded sound after a certain time limit, or have a stop recording button and then a 'Play' button to play the recorded clip.
Any ideas on how I can implement this?
The obvious solution would be to save the sequence of user actions in an NSArray, and play them back to recreate the sounds. I'm not sure that you can record the output sound channel into another file, if that's what you're trying to do.
Use the Speak Here example project provided by apple. Why reinvent the wheel?