Has anyone experienced issues with the AVAudioPlayer using an iPhone 5? My code has worked properly on the iPhone 4, iPhone 4S, and iPad (3rd gen) but it is randomly not working on the iPhone 5 now. I saw this question but no one addressed the actual issue:
AVAudioPlayer is not working in iPhone 5
NSURL *soundURL = [self urlForAlarmSong:songKey];
NSError *err;
self.audioAlert = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&err] autorelease];
[self.audioAlert prepareToPlay];
currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];
[self.audioAlert play];
audioAlert is an AVAudioPlayer i've declared in my header with (retain,nonatomic). The err variable is null everytime, even when it does not play on an iPhone 5. This code works flawlesly every time on an iPhone 4, iPhone 4S, and iPad (3rd gen).
Does anyone have any ideas?? Thanks
-(void)viewDidLoad
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"click2" ofType:@"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if(error)
{
NSLog(@"error in audio player:%@",[error localizedDescription]);
}
else
{
audioPlayer.delegate=self;
[audioPlayer prepareToPlay];
}
}
on button action,
-(IBAction)SoundButton:(id)sender
{
[audioPlayer play];
}
In your .h File, don't forget to import
#import AVFoundation/AVFoundation.h
and add a delegate AVAudioPlayerDelegate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With