iPhone sound playback
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I'm using Audioservice to try and play back sound, but sadly I always get -1500 as return value from AudioServicesCreateSystemSoundID.
My code:
OSStatus error;
error = AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: @"Glass.caf"], &firstSound);
AudioServicesPlaySystemSound (firstSound);
error = AudioServicesCreateSystemSoundID(CFURLCreateWithFileSystemPath(NULL, CFSTR("Tink.aif"), kCFURLHFSPathStyle, FALSE), &otherSound);
I tried several different approaches, but none of them worked. Anyone who can tell me what I am doing wrong?
Message was edited by: klapy
15 replies
Sign in or join Beachball Help to reply.
- 0
Steve Patt ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Your path is wrong; you need to look in your bundle, not in SystemPath.
- 0
Morti176 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi,
I'm a very new iPhone developer and stumbled exactly in this trap. My custom sound is beeing played back fine in the simulator, but once it comes to the real device it is not playing and reporting a -1500 error.
I have a test sound called "test.caf" in the root project folder and one test.caf is located in a subfolder "sounds". I tried several test code fragments, but none works on the phone. Code examples:
SystemSoundID soundId = 1024;
CFURLRef myURLRef=CFURLCreateWithFileSystemPath(kCFAllocatorDefault,CFSTR("test.caf"),kC FURLPOSIXPathStyle,FALSE);
OSStatus error = AudioServicesCreateSystemSoundID(myURLRef, &soundId);
NSLog(@"Load sound result: %d ", error);
AudioServicesPlaySystemSound(soundId);Result from the log file:
Wed Jul 16 19:28:36 unknown TeaTimer[393] <Warning>: Load sound result: -1500
You said something about loading it from the bundle correctly instead of a path? What is the correct way to load the sound file?
Any help GREATLY appreciated!!! 🙂
Martin
- 0
Scott Squires1 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
if it's a resource file try
NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"sun"
ofType:@"png"];The iPhone Programmers Guide has a section on file access. Likewise many of the sample code does as well.
- 0
Yibi ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi,
Is it the same when the sounds are in a folder inside the resources? Also, What parameter do I pass as SystemSoundId on the AudioServicesCreateSystemSoundID function? Your help is appreciated.
Thanx
- 0
sptrakesh ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You pass in the address of the sound id variable to the create function. The function will populate the value for you.
- 0
Yibi ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
and if the sound is inside a folder in the resources?
- 0
sptrakesh ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Something along these lines should work:
#define SOUND_DIRECTORY @"files/sounds"
#define CLICK_FILE @"button"
NSBundle *bundle = [NSBundle bundleForClass:[SudokuAppDelegate class]];
NSString *file = [bundle pathForResource:CLICK_FILE ofType:SOUND_TYPE
inDirectory:SOUND_DIRECTORY];
NSURL *url = [NSURL fileURLWithPath:file];
AudioServicesCreateSystemSoundID( (CFURLRef) url, &clickId ); - 0
Lee2 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi,
I tried using AudioServicesCreateSystemSoundID but got a link error. Any ideas?
"_AudioServicesPlaySystemSound", referenced from:..."
Regards,
Lee - 0
Lee2 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi,
It's ok, found the solution to my problem in another post
http://discussions.apple.com/thread.jspa?messageID=7370017�.Regards,
Lee - 0
Yibi ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Still don't understand what variable do I pass on the second parameter of the AudioServicesCreateSystemSoundID function.... In this thread one passes &clicked, but I have no Idea where he defined that.
Thanx
- 0
sptrakesh ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You would declare that as "SystemSoundID clickId" somewhere in your class that you can reuse later if you choose.
- 0
Yibi ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
NSBundle *bundle = [NSBundle bundleForClass:[SudokuAppDelegate class]];
produces a nil value, hence NSString *file also produces a nil value and *url crashes the application.
Of course, I changed the appdelegate to mine...
Playing a sound has become the biggest headache of mine this week. - 0
skinnytron ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
It took some testing but this finally worked for me.
First I had to import the sound into the resources folder of my application. (right click on the Resources folder and select "Add -> Existing Files..)
Then insert the following code.
SystemSoundID whipID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"DryWhip" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:soundFile];
OSStatus error = AudioServicesCreateSystemSoundID( (CFURLRef) url, &whipID );
AudioServicesPlaySystemSound(whipID);
The file I used is obviously called DryWhip.wav
- 0
Yibi ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I found this tutorial that really helped me out:
http://www.iphonedevcentral.org/tutorials.php?page=ViewTutorial&id=50&uid=573489 70
enjoy
- 0
JasonLV ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
When running on the device, I am getting the same error (-1500) when using AudioServicesCreateSystemSoundID . It works fine on the Simulator. I have tried copy/pasting the relevant sample code (and several variations of sample code) to a simple test project but I still get the same error. The sample apps like metronome work just fine on the device. I have tried with a small CAF file and a small WAV file. I have verified that the path and URL are correct prior to calling AudioServicesCreateSystemSoundID.
I have tried using all the code that people say works but I continue to get the same error. I'm baffled. Has anyone gotten this particular error and found a solution for it?
Any suggestions?
Thanks,
Jason