2013-03-03 53 views
0

我在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

按下按鈕加觸摸

[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); 
} 
+0

十分感謝,它的工作原理。 – Mihai 2013-03-03 14:23:59