longer than five seconds iphone sounds - iphone

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.

Related

playing a sound doesn't work on iPhone

I've added a sound to my app.
In my .h i've added:
CFURLRef soundFileURL;
SystemSoundID soundFile;
in my viewDidLoad in my .m:
soundFileURL = CFBundleCopyResourceURL(
CFBundleGetMainBundle(),
CFSTR("sound"),
CFSTR("mp3"),
NULL);
AudioServicesCreateSystemSoundID(
soundFileURL,
&soundFile);
and lastly i've added a -playSound method:
-(void)playSound {
NSLog(#"playSound");
AudioServicesPlayAlertSound(soundFile); }
It works fine on the iPhone Simulator, but when I build the app on my iPhone the console says the sound was played but it wasn't.
I read that many others had this problem too but I didn't find any solutions.
What's wrong?
It could be your encoding - the audio codecs on the simulator are much different than those provided by your actual iPhone hardware. Try reencoding.
Also - you don't want to use an mp3 for a sound, you want to use a non-compressed file format (I suggest aiff from experience) because there isn't dedicated decoding hardware to support the sound decoding you're trying to do. (There is for playing music, which mp3 is recommended for)
I would recommend AVAudioPlayer in the AVFoundation framework. It has a simple asynchronous interface for sound playback on iOS.
Check out the Apple programming guide here.
I had a similar issue. It turned out I was referencing "Horn.caf" with
NSURL *hornSound = [[NSBundle mainBundle] URLForResource: #"horn"
withExtension: #"caf"];
Note the difference in case; My OS X Lion install is on a case insensitive file system.
Changing the code to the following fixed the issue.
NSURL *hornSound = [[NSBundle mainBundle] URLForResource: #"Horn"
withExtension: #"caf"];
Hope that helps others.

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

Sound on simulator but not device

I'm using the following to play an m4a file:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: fileName];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
It works fine on the simulator but I hear nothing on the device. Sounds files I'm using all stay in the bundle. Here is what filePath looks like from the device:
file://localhost/var/mobile/Applications/418945F3-3711-4B4D-BC65-0D78993C77FB/African%20Adventure.app/Switch%201.m4a
Is there an issue with the file path or any thing different I need to do for the device?
Just as a sidenote - I was having the exact same problem and spent probably close to an hour on converting files to the correct format, etc.. Yet the problem was the "mute" switch on the iPad. So even though the volume was up, and I could hear other sounds on the iPad, because the mute switch was turned on, it wasn't playing system sounds.
To add to the confusion, this app uses text-to-speech and the volume coming from the dictation was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that weren't being played.
I had trouble with this too. Finally I realised it was because AudioServices can only play audio with the following constratints.
Sound files that you play using this
function must be:
- No longer than 30 seconds in duration
- In linear PCM or IMA4 (IMA/ADPCM) format
- Packaged in a .caf, .aif, or .wav file
From Apple docs: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html
You might want to use the AVAudioPlayer instead of AudioServices.
The following code will take an audio file (.m4a) and play the audio file 1 time. Don't forget to release "audioPlayer" when you're done with it.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"m4a"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
{
NSLog([error description]);
}
else
{
[audioPlayer play];
}
Hope this example helps you with playing audio on the actual device. It might also be a good idea to increase the device audio when the file is playing.
Note: You will need to add the AVFoundation framework to your project if you have not already done so. As well as import the header file.
#import <AVFoundation/AVFoundation.h>
Update:
From Apple's Core Audio Overview Document
Audio Session Services
Audio Session Services lets you manage audio sessions in your application—coordinating the audio behavior in your application with background applications on an iPhone or iPod touch. Audio Session Services consists of a subset of the functions, data types, and constants declared in the AudioServices.h header file in AudioToolbox.framework.
The AVAudioPlayer Class
The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application does not require stereo positioning or precise synchronization, and if you are not playing audio captured from a network stream, Apple recommends that you use this class for playback. This class is declared in the AVAudioPlayer.h header file in AVFoundation.framework.
Start by error-checking your returns. Is filePath nil? Do either of the AudioServices functions return an error? The most likely cause is case-sensitivity. The iPhone filesystem is case sensitive while the Mac is not. But the first step in debugging is to look at the errors the system is providing.
The simulator uses regular QuickTime for playback, so it's easy to have media assets which work in the sim, but fail on the device due to missing / unsupported codecs. The test is if you can play the file at all on the device, eg through Safari or the iPod app.

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.

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.