I have a huge Xcode project, which I have been working on an update to. After installing the iOS 7.1 SDK, playing audio on the AVAudioPlayer in this project no longer works. I created a new, blank project to test out the exact same code, and it worked perfectly.
I know for certain that the file is copied under the bundle resources, the file is added to the target, the URL is perfect because I was able to get the NSData from the NSURL of the file, and it matched. The AVAudioPlayer is a property with both the strong and nonatomic attributes, but it will not play in this one project. I also made sure to set the AVAudioSession to playback mode.
I even created some blank view controller classes to test out the AVAudioPlayer in the project, and it would not work in any of them, but in the new, blank iOS project I made, the sounds plays fine.
In the .h
NSURL *soundUrl;
@property (nonatomic, strong) AVAudioPlayer *soundPlayer;
In the .m
@synthesize soundPlayer = _soundPlayer;
- (void)playSomeSound {
NSError *audioError;
soundUrl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:soundFile ofType:@"m4a"]];
_soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&audioError];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
_soundPlayer.delegate = self;
[_soundPlayer play];
}
soundFile is just a string of the name of the sound file that I am trying to play.
I try to log for errors in all the delegate methods of the audio player and in the initialization of the audio player. All errors return (null).
Unlike on iOS 7, where the audio simply does not play, on iOS 6.1, initializing the audio player causes a crash with an EXC_BAD_ACCESS code 2.
Also, the AVAudioPlayer in CocosDenshion seems to work. (I have cocos2d used in parts of my project, but it is not a game.)
Another (possibly important) note is that I use AVAudioRecorder in my project as well. That works perfectly without any issues, and I make sure to switch the AVAudioSessionCategory to playback when I am not recording.