有没有办法在整个iOS应用程序中将特定的音频文件设置为UIButtons中的点击声音,而无需在单个视图中编写代码?
您不想在子类中添加任何代码。 所以你可以像这样子类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;
}
}
然后,每当人们触摸按钮控件时,您的自定义应用程序将收到此消息并播放声音。 您不必在任何单个视图中编写任何代码。
您可以通过在 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 框架
我会说,创建一个"观察者"类,用于播放所有按钮连接的声音。 检查此示例。
创建一个类别是要走的路。 回答者:蒂格罗
.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];