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.
Related
I know that you can for example play music in you app and it would be running in background but it looks like it's not working. I've this code when my application starts on iPhone:
pathToAudio = [[NSBundle mainBundle] pathForResource:#"silence" ofType:#"mp3"];
audioURL = [NSURL fileURLWithPath:pathToAudio];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Result is that it plays music when I send app to background but other activities doesnt run. For example in my app I'm listening to socket and although music plays in background my app doesnt respond to incomming messages (it normally responds when app is in foreground). What do I do wrong? I've read that you can do other activities when playing music when app is in background but it looks like it doesn't work...
Edit: I have jailbroken iPhone so any unconventional methods are welcome. I know that Apple would reject my app if I was playing silent audio in background and I dont care about it. I just want to know how to make app running in background when playing music or in any other way... Maybe I need to have iPhone 4? I have iPhone 3G.
Nothing, it's only possible to run audio in background (aside with some special cases like geoloc notifs & VoIP), not other stuff.
You'll need to provide a specification for the info.plist file like the UIBackgroundModes.
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.
Have already tried that out and failed
I put a .wav file into my app and it was fine. So I tried to put a .wav song file into the app. When runs, there's no sound coming out. ( the song is converted from .mp3 using iTunes)
Any ideas how can I fix this? Thanks in advance
one possible reason the sound file is too large. You have different kinds of audio player in iOS some of them can only play files that are really short (something like 1-3 sec). To fix that you have to choose another player like CoreAudio.
The second possible failure is the simulator. The simulator is not always doing the same, the device do. Try running on real device and check if it's working.
// EDIT: sample code:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"mySound" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL URLForString:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL];
[player play];
[player pause];
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).
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.