The Problem
I’m trying to play a short (4 second) audio clip in an iOS app.
- On the device (an iPhone 3G with iOS 4.2.1) I get silence. However, the vibrate does trigger as needed.
- In the simulator, the audio plays as expected.
I’m running in a Lion VM instance 10.7.2 on VMWare on top of Snow Leopard 10.6.8.
What I’ve tried
- Checking the format – it’s a linear PCM file
- Converting the file to a 16 bit audio file (Audacity reckoned it was 32 bit)
- Made sure the case of the filename is identical between code and files.
- Cleaned the target, deleted it from iPhone and rebuilt.
Code
// Usage from class
-(void)loadRunningView {
JGTimerRunningViewController *runningViewController = [[JGTimerRunningViewController alloc] initWithNibName:@"JGRunningViewController" bundle:nil];
[runningViewController loadAlertSoundWithFilename:@"DigitalAlarm1"];
}
@interface JGTimerRunningViewController : UIViewController {
IBOutlet UILabel *timeLabel;
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
}
@implementation JGTimerRunningViewController
-(void)loadAlertSoundWithFilename:(NSString *)alertSoundFilename_ {
// Get the main bundle for the app
CFBundleRef mainBundle = CFBundleGetMainBundle ();
CFStringRef filename = (CFStringRef)alertSoundFilename_;
soundFileURLRef = CFBundleCopyResourceURL (mainBundle, filename, CFSTR ("aiff"), NULL);
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);
}
..
-(IBAction)playSound:(id)sender {
AudioServicesPlaySystemSound (soundFileObject);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
-(void)showRedCard {
[[self view] setBackgroundColor:[UIColor redColor]];
[self playSound:self];
}
...
- (void)dealloc {
CFRelease(soundFileURLRef);
AudioServicesDisposeSystemSoundID(soundFileObject);
[timeLabel release];
[super dealloc];
}
@end
It turns out that somehow I had the volume turned down on the iPhone!