我正在为我的应用程序构建自定义键盘扩展。当用户单击一个键时,自定义音调称为" Tock.mp3"应该玩。它与系统音调不同。
我有4个Objective-C类和一个.xib文件。他们称为KeyboardViewController.h / .m,Keyboard.h / .m / .xib
我会在最后附上课程的代码。 所以,我尝试了以下内容:
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"Tock"
ofType:@"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:0.15f];
[click play];
我的问题:我需要什么代码以及在哪里实现它?
这是KeyboardViewController.h的代码:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface KeyboardViewController : UIInputViewController
@end
KeyboardViewController.m:
#import "KeyboardViewController.h"
#import "Keyboard.h"
@interface KeyboardViewController ()
@property (strong, nonatomic)Keyboard *keyboard;
@end
@implementation KeyboardViewController
- (void)updateViewConstraints {
[super updateViewConstraints];
// Add custom view sizing constraints here
}
- (void)viewDidLoad {
[super viewDidLoad];
// Perform custom UI setup here
self.keyboard = [[[NSBundle mainBundle] loadNibNamed:@"Keyboard" owner:nil options:nil]objectAtIndex:0];
[self addGesturesToKeyboard];
self.inputView = self.keyboard;
;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated
}
- (void)textWillChange:(id<UITextInput>)textInput {
// The app is about to change the document's contents. Perform any preparation here.
}
- (void)textDidChange:(id<UITextInput>)textInput {
// The app has just changed the document's contents, the document context has been updated.
UIColor *textColor = nil;
if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
textColor = [UIColor whiteColor];
} else {
textColor = [UIColor blackColor];
}
}
#pragma mark Keyboards
-(void)addGesturesToKeyboard
{
[self.keyboard.backKey addTarget:self action:@selector(pressDeleteKey) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.leerzeichenKey addTarget:self action:@selector(pressLeerzeichenKey) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.enterKey addTarget:self action:@selector(pressEnterKey) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.numbersKey addTarget:self action:@selector(pressNumbersKey) forControlEvents:UIControlEventTouchUpInside];
//Change to next Keyboard
[self.keyboard.globeKey addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
for (UIButton * key in self.keyboard.KeysArray) {
[key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void)pressDeleteKey
{
[self.textDocumentProxy deleteBackward];
}
-(void)pressLeerzeichenKey
{
[self.textDocumentProxy insertText:@" "];
}
-(void)pressEnterKey
{
[self.textDocumentProxy insertText:@"\n"];
}
-(void)pressKey: (UIButton*) key
{
[self.textDocumentProxy insertText:[[key currentTitle]lowercaseString]];
}
-(void)pressNumbersKey
{
}
@end
Keyboard.h:
#import <UIKit/UIKit.h>
@interface Keyboard : UIView
@property (strong,nonatomic)IBOutletCollection(UIButton)NSArray *KeysArray;
@property (weak, nonatomic) IBOutlet UIButton *upKey;
@property (weak, nonatomic) IBOutlet UIButton *numbersKey;
@property (weak, nonatomic) IBOutlet UIButton *globeKey;
@property (weak, nonatomic) IBOutlet UIButton *leerzeichenKey;
@property (weak, nonatomic) IBOutlet UIButton *enterKey;
@property (weak, nonatomic) IBOutlet UIButton *backKey;
@end
最后,我的Keyboard.m(不是很多!:D):
#import "Keyboard.h"
@implementation Keyboard
@end
答案 0 :(得分:0)
在 KeyboardViewController.m 中,我会创建一个这样的方法:
-(void)playSound
{
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Tock"ofType:@"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:0.15f];
[click play];
}
然后我会在每个事件处理程序中调用此方法:
-(void)pressDeleteKey
{
**[self playSound];**
[self.textDocumentProxy deleteBackward];
}
-(void)pressLeerzeichenKey
{
**[self playSound];**
[self.textDocumentProxy insertText:@" "];
}
-(void)pressEnterKey
{
**[self playSound];**
[self.textDocumentProxy insertText:@"\n"];
}
-(void)pressKey: (UIButton*) key
{
**[self playSound];**
[self.textDocumentProxy insertText:[[key currentTitle]lowercaseString]];
}
-(void)pressNumbersKey
{
**[self playSound];**
}
我没有使用AVAudioPlayer来播放声音,而是将声音文件转换为 .caf 文件。要播放 .caf 文件,请参阅Easiest way to play a CAF sound on the iPhone?
希望这有帮助!