作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的应用程序构建自定义键盘扩展。当用户单击一个键时,将播放一个名为“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];
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface KeyboardViewController : UIInputViewController
@end
#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
#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
#import "Keyboard.h"
@implementation Keyboard
@end
最佳答案
在 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];**
}
关于ios - 在自定义键盘上键入时播放自定义声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267888/
我是一名优秀的程序员,十分优秀!