简体   繁体   中英

AVAudioPlayer doesn't play audio in iPhone 5 when locked

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.

In the -Info.plist in Required background modes I wrote

App plays audio or streams audio/video using AirPlay

in Player.h implemented:

@property (nonatomic, strong) AVAudioPlayer *notificationPlayer;

<AVAudioPlayerDelegate, AVAudioSessionDelegate> with #import <AVFoundation/AVFoundation.h> included

Also in Player.m

//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:&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"]];
}

Regards.

Another Really important thing to note while playing sounds in background is whether or not you have permission to the file. If you are playing a bundle file, you have permission, and presumably some file streamed from the internet, but if you've downloaded media into the documents directory, then you must set permissions appropriately, especially if your application natively locks files.

This tends to match with what many people see as a symptom where the file will continue to play for a couple of seconds, 5-15, then stop. If you're listening for the finished method, you'll see an error flag. Why: AVAudioPlayer won't load the entirety of your audio file into memory when you first play it.
Example:

  NSURL url = [NSURL URLWithString:@""];

  NSError *error = nil;
  [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey :
  NSFileProtectionCompleteUntilFirstUserAuthentication} ofItemAtPath:[url path] error:&error];

Your options are as follows:

  NSFileProtectionComplete;
  NSFileProtectionCompleteUnlessOpen;
  NSFileProtectionCompleteUntilFirstUserAuthentication;
  NSFileProtectionNone;

I should use the following code

- (void)playAudio:(NSString *)path {
NSError *error;
NSURL *audioURL = [NSURL fileURLWithPath:path];

self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error];
self.notificationPlayer.delegate = self;

AVAudioSession* audioSession = [AVAudioSession sharedInstance];
NSError *errorSession = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil];
[audioSession setActive:NO error:&errorSession];

[self.notificationPlayer prepareToPlay];
[self.notificationPlayer setVolume:1.0];
[self.notificationPlayer play];
if (error) {
    NSLog(@"error %@",[error localizedDescription]);
}
NSLog(@"Should play music");
}

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player stop];
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM