Search code examples
objective-cavaudioplayerwave

AVAudioPlayer refuses to play anything, but no error, etc


This is the simplest possible AVAudioPlayer code, and it's just failing to play anything back. No errors, nothing in the console at all, the file is definitely being found as if I change the URL string to something which isn't there, I do get a crash. What am I doing wrong here? I've tried it with and without the delegate, as well as with and without prepareToPlay, and I can't get anything out. I've tried various sound files, too. Really tearing my hair out on this one!

@implementation ViewController

- (IBAction)playSound:(id)sender
{
    NSURL *soundLocation = [[NSURL alloc] initWithString:@"Lot01.wav"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundLocation error:nil];
    [audioPlayer setDelegate:self];
    [audioPlayer play];
}

@end

Solution

  • Turned out it was an issue with ARC releasing, I fixed it by adding a @property and @synthesize pair for the AVAudioPlayer in question, and declaring it as strong. It got rid of all of these errors, and played the file with no problems.