Hi, I am trying to make an app where pressing a button will play a random sound. I have the code written and am getting no errors, however the sounds don't play on my iPhone that I'm testing it with.
I've connected the button to the IBAction I want it to perform, but when it's tapped I get nothing.
Here is my Main.m code
My audio files are named like this "Sound 01.wav" if anyone can see my error, please point it out! Thanks!
I've connected the button to the IBAction I want it to perform, but when it's tapped I get nothing.
Here is my Main.m code
Code:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (AVAudioPlayer *)setupAudioPlayerWithFile:(NSString *)file type:(NSString *)type
{
NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:type];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!audioPlayer) {
NSLog(@"%@",[error description]);
}
return audioPlayer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)namesPressed{
int randomNumber = arc4random() % 68 + 1;
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound %02d", randomNumber] ofType:@"wav"]];
AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[soundPlayer setVolume:0.3];
[soundPlayer prepareToPlay];
[soundPlayer play];
}
@end
My audio files are named like this "Sound 01.wav" if anyone can see my error, please point it out! Thanks!