-2

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

** 大修问题** 在 Stack Overflow 上的这段时间里,我学到了很多东西,不仅仅是关于我通常需要帮助的内容(Objective-C,xCode),还有 Stack Overflow 的工作原理。我发现它提供了大量的信息和支持,并且我已经研究过我之前是如何提出这个问题的,但它完全没有任何意义。为了帮助未来的用户,我以其他人可以理解的方式编辑了我最初要求的内容,就像下面的 Naveen 所做的那样。道歉

4

2 回答 2

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

于 2012-05-18T08:06:49.607 回答
2

我想说,创建一个“观察者”类来播放所有按钮都连接到的声音。我将在下面举一个例子,它是一个单例类。它是一时兴起写的,没有经过测试,但会给你一个想法。

//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];
}
于 2012-05-18T03:27:20.097 回答