Reputation: 988
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
Upvotes: 3
Views: 626
Reputation: 486
-(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
.
Upvotes: 1
Reputation: 2008
Make sure that the device's sound switch is not in "mute" and the volume is high enough.
Looks pretty obvious but it can save hours of stress.
Upvotes: 0