Question

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
Was it helpful?

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.

OTHER TIPS

It seems like it's not correctly getting the path to your file, maybe try something like this.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Lot01" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:filePath];

then init the AVAudioPlayer exactly how you were except in that case it'd be withContentsOfURL:url.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top