views:

5426

answers:

5

I'm trying to play a sound file from an iPhone program.

Here's the code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"play" ofType:@"caf"];
NSFileHandle *bodyf = [NSFileHandle fileHandleForReadingAtPath:path];
NSData *body = [bodyf availableData];
NSLog( @"length of play.caf %d",[body length] );
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSLog( [url description] );
NSLog( @"%d", AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID) );

The first NSLog is to check that I have access to the file (I did), the second NSLog is to show the file URL, and the third NSLog returns -1500 "An unspecified error has occurred."

For the second NSLog, I get the following output:

file://localhost/Users/alan/Library/Application 敲慬楴敶瑓楲杮upport/iPhone蒠ꁻތĀ⾅獕牥⽳污湡䰯扩慲祲䄯灰楬慣楴湯匠灵潰瑲椯桐湯⁥楓畭慬潴⽲獕牥䄯灰楬慣楴湯⽳䙂㕅㡂㤱䌭䐳ⴸ䐴䙃㠭㍃ⴷ䍁㈶㠵䙁㤴㈰䰯捯瑡䵥⹥灡⽰汰祡挮晡imulator/User/Applications/BFE5B819-C3D8-4DCF-8C37-AC6258AF4902/LocateMe.app/play.caf

This is either due to my misunderstanding of the "description" method, or this contributes to the problem.

Any idea what is going wrong?

A: 

I tried the same with my application that plays sounds just fine. The sounds don’t play in the Simulator and when I try to NSLog the URL, I get the same garbage as You and EXC_BAD_ACCESS on the top of it. When I log the URL on the device, the URL is fine, but I get EXC_BAD_ACCESS nevertheless. When I drop the logging, sounds play and everything works. If somebody could explain this behaviour I’d be grateful. As for Your problem, I’d drop the logging and try the code on the device.

zoul
thanks for the comments. Good to hear that somebody else has the problem. I don't have my keys to try on the real device yet.
Alan
+1  A: 

The first parameter to NSLog is a format string; you're passing [URL description] as the format string to the second use of NSLog. That's bad, because if the description of the URL contains any % characters then it will wind up printing random stuff from the stack.

Instead, write

NSLog(@"%@", URL);

You don't need to even use -description here; NSLog will invoke it for you automatically because %@ means "an object," not "an NSString," and it's smart enough to do the right thing for you.

Chris Hanson
Thanks for this. I have to admit that I'm trying to learn this the same way many people learn JavaScript - ie hunt for examples, and copy/paste!
Alan
good catch Chris!
curtisk
A: 

An update on this.

I got my keys to allow me to run my app on the device.

I found that my .caf file successfully played when the app ran on the device.

I then tried various alternatives.

A .wav file that is 60 seconds long failed to work on the simulator and the device.

A .wav file that is 5 seconds long would work on the simulator, but not on the device.

A .aiff file that is 5 seconds long works on the simulator and the device.

It would be good to know definitively what the simulator and the device are checking about the file when it is passed to AudioServicesCreateSystemSoundID

Alan
+2  A: 

In addition, it seems that the iPhone simulator does not like to play (systemSound) sounds over 5 seconds in length. I had the same issue as out apps almost always play a start up systemSound.

One of the apps I was working on the simulator would not play the sound but the device would. Later I found that there is some strange limitation on the simulator and the way it handles sounds. I've filed a bug with Apple. Hopefully this will be address with the release of the iPhone SDK 3.0.

Kevin Bomberry
A: 

Use "AVAudioPlayer" class for playing .caf files

Biranchi