按下图像按钮时添加声音

时间:2013-03-03 13:57:26

标签: ios xcode image audio button

我在scrollView中有这个按钮,按下它时我需要发出声音。

UIButton *profileButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [profileButton setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateNormal];
    [profileButton setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateHighlighted];
    profileButton.frame = CGRectMake(0, 620, 320, 250);

[self.scrollView addSubview:profileButton];

我怎么能这样做? 谢谢,

1 个答案:

答案 0 :(得分:1)

按下按钮

[profileButton addTarget:self action:@selector(onButtonPress) forControlEvents:UIControlEventTouchUpInside];

//需要这个包含文件和AudioToolbox框架

#import <AudioToolbox/AudioToolbox.h>

播放一些声音

-(void) onButtonPress {
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"mp3"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
    AudioServicesPlaySystemSound (soundID);
}