Stop sound in iPhone - 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.

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?

Make AVFoundation not stop iPod music

Im using the AVFoundation framework to play sound files. The problem im having is that its stopping music from playing when the audio file gets used, im not saying play both files continuously, but play the sound file, then pick up the ipod music right where it left off. Is there any way i can use AVFoundation is this kind of way? or is there a better framework for it?
Here is what my code looks like:
click = [NSURL fileURLWithPath:[NSString stringWithFormat:#"#/Click.WAV", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:click error:nil];
audioPlayer.numberOfLoops = 1;
[click release];
[audioPlayer play];
This code works completely fine, i had to type it out so ignore any problems that there might be with it.
Thanks,
Jacob
You can use the AVAudioSession class to change the audio "category" of your app: thus you can allow it to play on top of the iPod music. Use the -setCategory:error: method, and you will probably want to use AVAudioSessionCategoryAmbient. More info can be found in the Audio Session Programming Guide.

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).

longer than five seconds iphone sounds

Hey everyone. The following is the code that I am using to play random sounds on the iPhone simulator. Some of the 30 or so sounds won't play and after some investigating I found out that the ones that weren't playing were longer than five seconds. Does anyone know why this is? Thanks in advance for your help!
NSString *path = [[NSBundle mainBundle] pathForResource:sound ofType:#"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
I believe there is a limit of 5 seconds (although some versions of the documentation claim 30 seconds) for sound being played using AudioServicesPlaySystemSound.
Alternatives are openAL, and Audio Queues. There's a useful tutorial on openAL here: http://benbritten.com/blog/2008/11/06/openal-sound-on-the-iphone/
The limitation should be only in the iPhone Simulator, not on the device. On the device, AudioServicesPlaySystemSound should play the sounds up to 30 seconds.
I believe the sound format/compression is what causes the 'artificial' 5 second limit. Use afconvert on the Terminal to convert the sounds to the Apple suggested formats.

Playing short sound on timer end?

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.