2

使用 AVAudioPlayer 我试图在 iphone 播放器播放时播放声音。同样当设备被锁定时。问题是,在 iPhone 4s ios 7 中 - 工作正常。但是在带有 6 和 7 ios 的 iPhone 5 上没有任何反应。

在我写-Info.plistRequired background modes

App plays audio or streams audio/video using AirPlay

Player.h实施:

@property (nonatomic, strong) AVAudioPlayer *notificationPlayer;

<AVAudioPlayerDelegate, AVAudioSessionDelegate>#import <AVFoundation/AVFoundation.h>包括_

也在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"]];
}

问候。

4

2 回答 2

2

在后台播放声音时要注意的另一件非常重要的事情是您是否有权访问该文件。如果您正在播放捆绑文件,则您有权限,并且可能是从 Internet 流式传输的某些文件,但是如果您已将媒体下载到文档目录中,则必须适当地设置权限,尤其是在您的应用程序本机锁定文件的情况下。

这往往与许多人认为的症状相匹配,即文件将继续播放几秒钟,5-15 秒,然后停止。如果你正在监听完成的方法,你会看到一个错误标志。原因:当您第一次播放时,AVAudioPlayer 不会将您的整个音频文件加载到内存中。
例子:

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

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

您的选择如下:

  NSFileProtectionComplete;
  NSFileProtectionCompleteUnlessOpen;
  NSFileProtectionCompleteUntilFirstUserAuthentication;
  NSFileProtectionNone;
于 2014-04-22T00:34:15.660 回答
1

我应该使用以下代码

- (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];
}
于 2013-10-26T13:27:48.460 回答