-->

Sound working in emulator, not in real iPhone

2019-07-17 16:20发布

问题:

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?

回答1:

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


回答2:

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 transcription was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that I couldn't hear.



回答3:

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



回答4:

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!



标签: iphone audio caf