Reputation: 175
I want to do that every time I click on the picture it will make sound. add at "Linked Frameworks and Libraries" AudioToolbox and at GameViewController.h add:
#import <AudioToolbox/AudioToolbox.h>
@interface GameViewController : UIViewController
{
SystemSoundID PlaySoundID;
}
- (IBAction)PlayAudioButton:(id)sender;
(connected this with button).
at GameViewController.m add this:
- (void)viewDidLoad
{
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Sound" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
}
- (IBAction)PlayAudioButton:(id)sender {
AudioServicesPlaySystemSound(PlaySoundID);
}
after all isn't working no error but sound not woking... what the problem?
thanks.
Upvotes: 0
Views: 1170
Reputation: 5812
Not sure if I understand your question correctly but this is how I'd get the custom sound to play when you tap the button
make sure to
#import <AVFoundation/AVFoundation.h>
in your .h or .m file
create an AVAudioPlayer *audioPlayer property for your class (either in .h or .m) and then do this:
-(void)viewDidLoad{
[super viewDidLoad]
NSError *error;
NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"wav"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *theAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudioPlayer prepareToPlay];
self.audioPlayer = theAudioPlayer;
}
- (IBAction)PlayAudioButton:(id)sender{
[self.audioPlayer play];
}
Upvotes: 1
Reputation: 820
Use this instead:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"yourSound"
ofType:@"mp3"]];
AVAudioPlayer sound = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[sound play];
Upvotes: 0