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

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

【问题讨论】:

    标签: ios iphone objective-c audio


    【解决方案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];
        

        【讨论】:

          猜你喜欢
          • 2014-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-29
          • 1970-01-01
          • 1970-01-01
          • 2012-06-01
          相关资源
          最近更新 更多