Luke
Luke

Reputation: 9700

Using AVAudioPlayer in iOS5?

So I'm trying to play a .wave file in iOS5, and I'm getting a warning, which is leading to SIGABRTs and generally not-working code.

NSURL *soundUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Resources/Sounds/01_main_menu_list.wav", [[NSBundle mainBundle] resourcePath]]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer setDelegate:self];
[_audioPlayer play];

This is giving me an error "Sending "ViewController *const __strong" to parameter of incompatible type 'id '.

I have literally no idea why, I've followed a half-dozen examples and am coming up empty. I'd love a pointer in the right direction.

Upvotes: 0

Views: 632

Answers (2)

Luke
Luke

Reputation: 9700

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.

Upvotes: 0

Richard G. Nielsen
Richard G. Nielsen

Reputation: 1271

This should work:

NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"01_main_menu_list" ofType:@"wav"]]; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioPlayer.delegate = self;
[audioPlayer play];

If not, I think there's something wrong with the wav file. Maybe the encoding?

Upvotes: 2

Related Questions