Replace
NSLog(@"audioPlayer is nil");
with following code to get error information:
NSLog(@"audioPlayer init error:%@", error);
try this to implement the function:
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"m4a"];
NSError *error = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]
options:NSDataReadingMappedIfSafe
error:&error];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
if (audioPlayer == nil) {
NSLog(@"audioPlayer is init error:%@", error);
}