6

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.

4

6 回答 6

16

正如上面提到的 Farzad NazifiAllow openAcesess在全新安装后解决了我的问题。我推荐一个更简单的解决方案playing system keyboard tap sound

Swift 和 Objective-C 都一样`我们不需要导入任何自定义声音文件只播放系统敲击声音。

导入 AudioToolbox 框架:

#import <AudioToolbox/AudioToolbox.h>

AudioServicesPlaySystemSound(1104)

此代码在 iOS 8 beta 5 中测试,希望苹果能修复 GM 版本中的 bug(如果是 bug)。

于 2014-09-03T19:58:34.943 回答
9

最后我从其他 SO线程得到了答案。

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

我已经实现并验证了此方法适用于 iOS8 Beta 2 上的模拟器和设备。

于 2014-07-06T09:28:39.243 回答
8

导入框架:

#import <AudioToolbox/AudioToolbox.h>

并使用:

AudioServicesPlaySystemSound(1104);
于 2015-05-06T16:01:53.293 回答
1

播放声音需要 OpenAccess,我不知道为什么 Apple 会这样做,但现在就是这样。这也将解决延迟和尝试播放声音后无法正常工作的问题。

您可以通过在键盘的 info.plist 中将 RequestsOpenAccess Key 的值设置为 YES 来获取 OpenAccess。

接下来,您应该将 AudioToolbox 导入您的项目,然后使用以下代码:

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

现在安装您的键盘并在将其添加到键盘时提供 Openaccess,它应该可以工作。

于 2014-09-03T01:06:42.277 回答
1

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];

}
于 2014-07-02T17:04:33.943 回答
1

你可以使用playInputClick()方法。

迅速:

UIDevice.currentDevice().playInputClick()

目标-C:

[[UIDevice currentDevice] playInputClick];

有关详细信息,请参阅文档

于 2016-03-07T23:44:16.273 回答