How to play tock sound when tapping custom keyboar

2020-02-24 06:41发布

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.

6条回答
Evening l夕情丶
2楼-- · 2020-02-24 07:06

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.

查看更多
爷的心禁止访问
3楼-- · 2020-02-24 07:10

You can use playInputClick() method.

Swift:

UIDevice.currentDevice().playInputClick()

Objective-C:

[[UIDevice currentDevice] playInputClick];

See documentation for details.

查看更多
甜甜的少女心
4楼-- · 2020-02-24 07:20

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 .

查看更多
狗以群分
5楼-- · 2020-02-24 07:21

Import the framework:

#import <AudioToolbox/AudioToolbox.h>

And use:

AudioServicesPlaySystemSound(1104);
查看更多
Melony?
6楼-- · 2020-02-24 07:21

Playing sounds requires OpenAccess, I don't know why Apple is doing it like this but it is what it is for now. This will also fix the lag and issues with it not working after trying to play the sound.

You can get OpenAccess by setting YES for the value of RequestsOpenAccess Key in the info.plist of the keyboard.

Next you should import AudioToolbox to your project, then use this code:

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

Now install your keyboard and give the Openaccess while adding it to your keyboards and it should work.

查看更多
Animai°情兽
7楼-- · 2020-02-24 07:30

The key here is that iOS can only play the file types described here. iOS cannot play the file type .caf. The following code should work fine on iOS. You can use this website to convert your .caf file to any file they have available on the site and that are compatible. I've tested it out already and .caf conversions work even though it's not specified anywhere.

-(void)buttonPressed:(UIButton *)theButton {


    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"Tock" ofType:@"mp3"]] error:nil];
    [av setVolume:1.0];
    [av setNumberOfLoops:0];
    [av prepareToPlay];
    [av play];

}
查看更多
登录 后发表回答