Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play tock sound when tapping custom keyboard buttons

I've been working on a custom keyboard for iOS 8 for some time and everything went fine so far, but I still couldn't get my head around this tapping sound stuff.

I searched high and low for this issue and tried several approaches including

  1. Using AudioToolbox
  2. Using AVFoundation
  3. Put the tock.caf inside my bundle and just play it

Some of them works, in the simulators but none of them works in my devices. Could anyone who has successfully played sound when tapping on custom keyboard buttons care to share some working code? And it is the best if the code could honor the sound settings.

like image 770
Evan Chu Avatar asked Jun 30 '14 18:06

Evan Chu


2 Answers

As Farzad Nazifi mentioned above Allow openAcesess after a fresh install , solved my issue . And I recommend a simpler solution for playing system keyboard tap sound

Swift and Objective-C All the same `we don't need to import any custom sound file just play the system tap sound.

Import AudioToolbox framework:

#import <AudioToolbox/AudioToolbox.h>

AudioServicesPlaySystemSound(1104)

This code is tested in iOS 8 beta 5 , I hope apple would fix the bug (if it is a bug) in the GM version .

like image 188
Ezimet Avatar answered Oct 13 '22 01:10

Ezimet


Finally I got an answer from other SO thread.

- (void)playSound
{
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Tock" ofType:@"caf"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
    AudioServicesPlaySystemSound (soundID);
}

I have implemented and verified this method works on both simulators and devices on iOS8 Beta 2.

like image 9
Evan Chu Avatar answered Oct 13 '22 00:10

Evan Chu