有没有办法在整个iOS应用程序中将UIButtons中的特定音频文件设置为点按声音而无需在个别视图中编写代码?
答案 0 :(得分:4)
您不希望在子类中添加任何代码。因此您可以将UIApplication子类化为:
@interface PlaySound : UIApplication
@end
@implementation PlaySound
- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event{
if ([sender isKindOfClass:[UIButton class]]) {
//here, play sound
}
return [super sendAction:action to:target from:sender forEvent:event];
}
@end
并在main.m
注册您自己的应用程序,如:
int main(int argc, char *argv[])
{
@autoreleasepool {
NSString *appClass = @"PlaySound";
int retVal = UIApplicationMain(argc, argv, appClass, NSStringFromClass([AppDelegate class]));
return retVal;
}
}
然后,当人们触摸按钮控件时,您的自定义应用程序将收到此消息并播放声音。您不必在任何单独的视图中编写任何代码。
答案 1 :(得分:3)
您可以通过在 AppDelegate 文件
中编写一次代码来实现此目的在 AppDelegate.m 中编写一个类方法,如下面给出的
+(void)playAlarmSound
{
NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:@"Output" ofType:@"aif"])){
NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
if (audioPlayer)
{
[audioPlayer release];
audioPlayer= nil;
}
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]autorelease];
audioPlayer.delegate = self;
[url release];
audioPlayer.numberOfLoops=0;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
}
在 AppDelegate.h
中+(void)playAlarmSound;
现在,在您要调用上述方法的班级中,请写下以下行
[AppDelegate playAlarmSound];
注意: - 导入AVFoundation框架
答案 2 :(得分:2)
我会说,创建一个'Observer'类,播放所有按钮连接的声音。检查this示例。
答案 3 :(得分:0)
创建一个类别是要走的路。由tiguero
回答·H:
#import <UIKit/UIKit.h>
@class SCLSoundEffect;
typedef enum {
SCLCLICKSOUND = 0,
SCLOTHERSOUND,
}
SCLSoundCategory;
@interface UIButton (soundEffect)
@property (nonatomic, strong) SCLSoundEffect *buttonSoundEffect;
+ (id) buttonWithType:(UIButtonType)buttonType andSound: (SCLSoundCategory)soundCategory;
- (void) playSound;
@end
的.m:
#import "UIButton+soundEffect.h"
#import <objc/runtime.h>
#import "SCLSoundEffect.h"
static char const * const kButtonSoundEffectKey = "buttonSoundEffect";
@implementation UIButton (soundEffect)
@dynamic buttonSoundEffect;
+ (id) buttonWithType:(UIButtonType)buttonType andSound:(SCLSoundCategory) soundCategory;
{
UIButton *newButton = [UIButton buttonWithType:buttonType];
NSString *stringToUse = nil;
switch (soundCategory) {
case SCLCLICKSOUND:
stringToUse = @"button_sound.wav";
break;
case SCLOTHERSOUND:
assert(0); // To be defined
default:
break;
}
[newButton setButtonSoundEffect: [[SCLSoundEffect alloc] initWithSoundNamed:stringToUse]];
[newButton addTarget:newButton action:@selector(playSound) forControlEvents:UIControlEventTouchDown];
return newButton;
}
- (void) playSound
{
[self.buttonSoundEffect play];
}
- (SCLSoundEffect *)buttonSoundEffect {
return objc_getAssociatedObject(self, kButtonSoundEffectKey);
}
- (void)setButtonSoundEffect:(SCLSoundEffect *)buttonSoundEffect{
objc_setAssociatedObject(self, kButtonSoundEffectKey, buttonSoundEffect, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void) dealloc
{
[self setButtonSoundEffect:nil];
}
Now each time I create a button that play some sound I just need to use the following method:
UIButton *mySoundButton = [UIButton buttonWithType:UIButtonTypeCustom andSound:SCLCLICKSOUND];