简体   繁体   English

锁定后,AVAudioPlayer无法在iPhone 5中播放音频

[英]AVAudioPlayer doesn't play audio in iPhone 5 when locked

Using AVAudioPlayer I'm trying to play sounds while the iphone player is playing. 我正在尝试使用AVAudioPlayer在播放iphone播放器时播放声音。 Also when device is locked. 同样在设备锁定时。 The thing is, that in iPhone 4s ios 7 - works fine. 事实是,在iPhone 4s ios 7中-可以正常工作。 But on iPhone 5 with 6 and 7 ios nothing happens. 但是在具有6和7 ios的iPhone 5上,什么也没有发生。

In the -Info.plist in Required background modes I wrote -Info.plistRequired background modes我编写了

App plays audio or streams audio/video using AirPlay

in Player.h implemented: Player.h实现:

@property (nonatomic, strong) AVAudioPlayer *notificationPlayer;

<AVAudioPlayerDelegate, AVAudioSessionDelegate> with #import <AVFoundation/AVFoundation.h> included 包含带有#import <AVFoundation/AVFoundation.h> <AVAudioPlayerDelegate, AVAudioSessionDelegate>

Also in Player.m 同样在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. 如果您正在播放捆绑文件,则具有权限,并且大概是从互联网流式传输的某些文件,但是如果您已将媒体下载到documents目录中,则必须适当设置权限,尤其是在您的应用程序本机锁定文件的情况下。

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. 这往往与许多人认为的症状相匹配,在这种症状下,文件将继续播放几秒钟,即5-15,然后停止播放。 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. 原因:第一次播放时,AVAudioPlayer不会将整个音频文件加载到内存中。
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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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