UIButton - 如何让它播放声音

时间:2012-05-18 02:05:27

标签: iphone objective-c ios5 xcode4.3

我在笔尖(xib)中有一个UIButton,它喜欢在触摸时播放声音,需要遵循什么协议来实现这一点?

**彻底改变了问题**在Stack Overflow的整个过程中,我学到了很多东西,而不仅仅是我通常需要帮助的东西(Objective-C,xCode),还有Stack Overflow的工作原理。我发现它是一个丰富的信息和支持,我已经看过我之前是如何提出这个问题的,它完全没有任何意义。为了帮助未来的用户,我编辑了我最初要求的内容,其他人可以理解,就像Naveen在下面所做的那样。道歉

2 个答案:

答案 0 :(得分:3)

添加此框架

AudioToolBox framework

包含此头文件.h文件

#include <AudioToolbox/AudioToolbox.h>
CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;

在viewdidLoad中添加此代码

NSURL *tapSound   = [[NSBundle mainBundle] URLForResource: @"Voice035"
                                            withExtension: @"amr"];

    // Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];

    // Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
                                  soundFileURLRef,
                                  &soundFileObject
                                  );

按钮点按,调用此方法

 AudioServicesPlayAlertSound (soundFileObject);

以dealloc发布

AudioServicesDisposeSystemSoundID (soundFileObject);
CFRelease (soundFileURLRef);

了解更多信息:http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html#//apple_ref/c/func/AudioServicesCreateSystemSoundID

答案 1 :(得分:2)

我想说,创建一个'Observer'类,播放所有按钮连接的声音。我将在下面给出一个例子,它是一个单例类。它是随心所欲地编写的,没有经过测试,但会给你一个想法。

// SoundObserver.h

#include <AVFoundation/AVFoundation.h>

@interface SoundObserver {

    AVAudioPlayer *audioPlayer;
}
-(void)playSound;
+(SoundObserver*)sharedInstance;

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

@end


//SoundObserver.m

static SoundObserver *instance = nil;

@implementation SoundObserver


-(id)init {

    self = [super init];

    if(self) {

  //Get path to file.
  NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sound"
                                                       ofType:@"wav"];

  // filePath to URL
  NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

  //Initialize the AVAudioPlayer.
  self.audioPlayer = [[AVAudioPlayer alloc]
                           initWithContentsOfURL:fileURL error:nil];

  // Preloads buffer and prepare for use.
  [self.audioPlayer prepareToPlay];

  [filePath release];
  [fileURL release];

    }

}

+(SoundObserver*)sharedInstance {

        @synchronized(self) {

        if (instance == nil)

            instance = [[self alloc] init];
    }

    return instance;
}


-(void)playSound {

    //make sure the audio resets to beginning each activation.

    [self.audioPlayer play];
}

-(void)dealloc {

    //Clean up
    [self.audioPlayer release];
    self.audioPlayer = nil;
    [super dealloc];
}



// User example.

In applicationDidFinishLaunching:

    [SoundObserver sharedInstance];

From here you can connect all buttons to the same function, or call it from anywhere in the App that #import's SoundObserver.h

-(IBOutlet)buttonClick:(id)sender {

    [[SoundObserver sharedInstance] playSound];
}
相关问题