tags:

views:

255

answers:

3

Have few short sound effect samples, which play just fine in emulator, but not at all in real iPhone 3GS. Here's the code, about as-is from Apple SysSound sample:

CFBundleRef mb = CFBundleGetMainBundle ();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL
    (mb, CFSTR("mySound"), CFSTR ("caf"), NULL);

SystemSoundID sid;
AudioServicesCreateSystemSoundID(soundFileURLRef, &sid);
AudioServicesPlaySystemSound(sid);

When using iPhone, I can hear keyclicks and music from iTunes (not trying to use at same time as playing my sound) - but cannot hear my sound at all. Vibra works ok, so even Framework should be set up correctly.

Tried even the SoundEffect.h/m sample code, no change. Used same sound files, but shouldn't CAF be ok, especially when it plays in emulator?

What can I try next?

A: 

kind of a long shot, but remember that the phone's file system is case sensitive, while the mac's usually isn't. Double check your file name

justin
I thought being *nix based it would be case-sensitive :)
willcodejavaforfood
One would think so, but that would make like way too easy. If you have a file "Foo", and write code to open it as "foo", it works in the simulator, fails on the device.
justin
clarifying a little bit
justin
A: 

Try converting to a different format such as wav or mp3, then play again. If you want to use caf, Make sure you are formatting the caf correctly in Terminal.app:

afconvert -f caff -d ima4 mysound.wav
coneybeare
Most likely I converted in wrong way... Still can't understand why emulator would play those sounds and real device not. Emulator is supposed to emulate real device, right? That's the purpose of emulator!
JOM
wrong. The simulator is not an emulator. It does not limit your network speed, cpu speed ram or anything else. It is merely a visual tool to see how you app would look and interact with touches. It is probably because your computer has many more codecs on it than the select few shipped with the the iphone
coneybeare
A: 

Found an easier solution: use AIF sound files:

  • Click iTunes > Preferences
  • Click on "General" tab
  • Click "Import Settings" button
  • In "Import Using" dropdown, choose "AIFF Encoder"
  • Save your changes
  • Select your sound files and choose "Create AIFF version"

Here's code I'm using, together with SoundEffect.h and SoundEffect.m from Apple sample BubbleLevel:

NSBundle *mainBundle = [NSBundle mainBundle];
buzzerSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"buzzerSound" ofType:@"aif"]];
[buzzerSound play];

Now same code - and sound effects - work in both emulator and hardware. Btw don't forget to switch back your original iTunes settings!

JOM