AVAudioPlayer doesn't play audio in iPhone 5 when locked

穿精又带淫゛_ 提交于 2019-12-24 00:58:21

问题


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.


回答1:


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;



回答2:


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];
}


来源:https://stackoverflow.com/questions/19570211/avaudioplayer-doesnt-play-audio-in-iphone-5-when-locked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!