I have this piece of code which plays my mp3 files on my iphone and it works:
if(i == 1) {
NSString *url = [[NSBundle mainBundle]pathForResource:@"fart" ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url] error:NULL];
[player play];
} else if(i==2) {
NSString *url = [[NSBundle mainBundle]pathForResource:@"fart6" ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url] error:NULL];
[player play];
} else {
NSString *url = [[NSBundle mainBundle]pathForResource:@"fart7" ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url] error:NULL];
[player play];
}
The only lines that are changing is the NSString *url. When I try to do the below to clean up my code, I get the error of "use of undeclared identifier 'url'".
if(i == 1) {
NSString *url = [[NSBundle mainBundle]pathForResource:@"fart" ofType:@"mp3"];
} else if(i==2) {
NSString *url = [[NSBundle mainBundle]pathForResource:@"fart6" ofType:@"mp3"];
} else {
NSString *url = [[NSBundle mainBundle]pathForResource:@"fart7" ofType:@"mp3"];
}
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url] error:NULL];
[player play];
Why does NSURL not understand that the NSString *url is set from the above if-else statements?