Reputation: 2950
I am trying to play the Tock sound for my custom keyboard.
What I have tried:
[[UIDevice currentDevice] playInputClick];
and
NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
and
AudioServicesPlaySystemSound(0x450);
and
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = 1;
[myAudioPlayer play];
This gave me an ERROR:
AudioSessionClient: Couldn't connect to com.apple.audio.AudioSession
Couldn't connect to com.apple.audio.AudioQueueServer; AudioQueue will not be usable
and also looked into this link
When I tried these codes in my ViewControllers (not the custom keyboard) it all worked. But when I tested it on my custom keyboard it idled for at least 10 seconds and I couldn't hear anything.
I also made sure that my iPhone is not on silent mode, speaker is not broken, keyboard clicks is enabled. I tested these codes on my iPhone and iPad and it turned out the same
Xcode Version 6.3.1 iOS Version 8.3
What am I doing wrong? Thanks in advance.
Upvotes: 1
Views: 541
Reputation: 2950
I found the solution to my problem.
In the Info.plist.
RequestsOpenAccess = YES
and with this code on my KeyboardViewController.m
dispatch_async(dispatch_get_main_queue(), ^{
AudioServicesPlaySystemSound(1104);
});
Upvotes: 2
Reputation: 5347
You have to implement the <UIInputViewAudioFeedback>
protocol on your input view. I.e. you have a keyboard (UIView
subclass) which has buttons on it, which implements this code:
- .h
file:
@interface RHTableKeyboard : UIView <UIInputViewAudioFeedback>
@end
.m
file:
- (BOOL)enableInputClicksWhenVisible
{
return YES;
}
Then you might subclass UIButton
and do something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if (highlightColour) {
origionalColour = self.backgroundColor;
[self.layer setBackgroundColor:[highlightColour CGColor]];
}
if (makeClickSound) {
[[UIDevice currentDevice] playInputClick];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if (highlightColour) {
[self.layer setBackgroundColor:[origionalColour CGColor]];
}
}
(I chucked in the code to make it change color as a bonus.)
Upvotes: 0