views:

1225

answers:

3

Hi there,

I have added the iPhone's Tock sound to my own custom keyboard like this:

NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
// we don't dispose of the sound to keep the sound in the cache for the next time
//AudioServicesDisposeSystemSoundID(soundID);

In iPhone OS 3.1, however, the keyboard Tock sound has been muffled, e.g. is much more silent than other sounds and than it was in 3.0. My method's sound is still very loud and at the maximum volume. How could I get the same volume as the iPhone's default keyboard?

By the way, the Tock sound in Twitterrific is muffled appropriately.

Cheers

MrMage

+2  A: 

Use AVAudioPlayer instead of AudioServices and you will have access to a volume property that allows you to lower and raise the volume of the sound.

mahboudz
And which volume do I have to set it to to mimic the iPhone's keyboard Tock?
MrMage
THe volume property is a float - anywhere between 0.0 and 1.0, although it seems like values over 1.0 will produce even higher volumes. As you probably notice, the keyboard sound changes as you change the iPhone's volume. I would start at 0.5 and see how that compares.
mahboudz
I used ProLevel (http://www.katsurashareware.com/pgs/prolevel.html) to measure output level from default keyboard and somewhere between 0.45 and 0.5 worked out
esad
+1  A: 

This is probably stupid, but I've seen in other sample code that there is also a tick sound file. And from the examples I've seen, it's ofType:@"caf", so that's worth a try as well. Sorry if I'm way off.

Jorge Israel Peña
+2  A: 

Using 0x450 as the SystemSoundID works for me at the correct volume - note that it doesn't respect the keyboard clicks on/off preference.

I'm not sure how portable this is - it works for me on the simulator and on an iPod Touch 3rd gen. I suspect it works on all devices but could change without notice in a software update.

I got this number by setting a breakpoint on AudioServicesPlaySystemSound and pressing a normal keyboard key - the first assembly instruction copies the SystemSoundID into a register, so you can see it in the Registers view in the debugger.

Sam McCall
Thank you, that's exactly what I wanted.
MrMage