AVAudioPlayer refuses to play anything, but no error, etc
Asked Answered
B

2

0

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
Bethink answered 20/2, 2012 at 14:5 Comment(0)
B
6

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.

Bethink answered 21/2, 2012 at 15:41 Comment(0)
B
1

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.

Bushel answered 20/2, 2012 at 14:28 Comment(1)
Okay, when doing that, there's still no audio output, but there's a short pause when clicking the button to trigger the IBAction, so that would suggest something's happening. I got a LOT of console output upon clicking too, lots of errors regarding unrelated Audio Unit plug-ins I have installed. Do I need to tell the iPhone Simulator which audio output device to use, or will it use the system default? I've tested on device too, and there's no sound there either.Bethink

© 2022 - 2024 — McMap. All rights reserved.