如何在整个应用程序中设置 UIButton 点击​​声音?

Posted

技术标签:

【中文标题】如何在整个应用程序中设置 UIButton 点击​​声音?【英文标题】:How to set UIButton tap sound in whole app? 【发布时间】:2013-12-26 05:39:17 【问题描述】:

有没有办法在整个 ios 应用程序的 UIButtons 中将特定的音频文件设置为点击声音,而无需在单个视图中编写代码?

【问题讨论】:

【参考方案1】:

您不想在 subclass 中添加任何代码。所以你可以继承 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;
    

然后,每当人们触摸按钮控件时,您的自定义应用程序都会收到此消息并播放声音。您不必在任何单独的视图中编写任何代码。

【讨论】:

【参考方案2】:

您可以通过在 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 框架

【讨论】:

感谢您的回复。但除非绝对必要,否则我更喜欢避免使用 AppDelegate。【参考方案3】:

我想说,创建一个“观察者”类来播放所有连接到的按钮的声音。检查this 示例。

【讨论】:

【参考方案4】:

创建一个类别是要走的路。由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];

【讨论】:

以上是关于如何在整个应用程序中设置 UIButton 点击​​声音?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ios 中使用图案图像在可扩展的 tableViewCell 中设置 UIButton

如何在 UITableViewCell 中设置 UIButton 状态?

如何在 Swift 3 中设置 UIButton 的图像?

在 AppDelegate 中设置 UIButton 外观时如何更改 SafariViewController 中导航栏的色调

在 Swift 中设置 UIButton 图像 - 当前解包为 nil

Swift 在情节提要中设置 UIButton setBorderColor