Note that there are some explanatory texts on larger screens.

plurals
  1. POAVAudioPlayer doesn't play audio in iPhone 5 when locked
    text
    copied!<p>Using AVAudioPlayer I'm trying to play sounds while the iphone player is playing. Also when device is locked. The thing is, that in iPhone 4s ios 7 - works fine. But on iPhone 5 with 6 and 7 ios nothing happens.</p> <p>In the <code>-Info.plist</code> in <code>Required background modes</code> I wrote </p> <p><code>App plays audio or streams audio/video using AirPlay</code></p> <p>in <code>Player.h</code> implemented:</p> <pre><code>@property (nonatomic, strong) AVAudioPlayer *notificationPlayer; </code></pre> <p><code>&lt;AVAudioPlayerDelegate, AVAudioSessionDelegate&gt;</code> with <code>#import &lt;AVFoundation/AVFoundation.h&gt;</code> included</p> <p>Also in <code>Player.m</code></p> <pre><code>//this is called on viewDidLoad -(void) prepareSound{ [[AVAudioSession sharedInstance] setDelegate:self]; } //overriden function - (void)playAudio:(NSString *)path{ [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: AVAudioSessionCategoryOptionDuckOthers error: nil]; NSError *error; NSURL *audioURL = [NSURL fileURLWithPath:path]; self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&amp;error]; self.notificationPlayer.delegate = self; [self.notificationPlayer prepareToPlay]; [self.notificationPlayer setVolume:1.0]; [self.notificationPlayer play]; if (error) { NSLog(@"error %@",[error localizedDescription]); } } - (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ [player stop]; [[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil]; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: 0 error: nil]; [[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil]; } //Call from here -(void) customMethod{ [self playAudio:[[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"]]; } </code></pre> <p>Regards.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload