Mihai
Mihai

Reputation: 131

adding a sound when pressing an image button

I have this button in the scrollView and i need to play a sound when pressing it.

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];

how could i do this? Thanks,

Upvotes: 0

Views: 203

Answers (1)

B.S.
B.S.

Reputation: 21726

adding touch on button press

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

// need this include file and the AudioToolbox framework

#import <AudioToolbox/AudioToolbox.h>

Playing some sound

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

Upvotes: 1

Related Questions