Cra*_*ray 4 objective-c core-audio avaudioplayer ios ios7
我正在尝试在收到远程通知时自动播放声音文件(这不是我的应用程序包的一部分,也不是通知声音)。我希望在收到通知时无论应用程序处于前台还是后台都将发生这种情况。
我正在使用Amazing Audio Engine作为核心音频库的包装。在我的App Delegate中,didReceiveRemoteNotification创建一个Audio Controller并添加AEAudioFilePlayer到其中,如下所示:
NSURL *file = [NSURL fileURLWithPath:sourceFilePath];
AEAudioFilePlayer *notificationPlayer = [AEAudioFilePlayer audioFilePlayerWithURL:file
audioController:_notificationController
error:NULL];
notificationPlayer.completionBlock = ^{
// Remove self from channel list after playback is complete
[_notificationController removeChannels:[NSArray
arrayWithObjects:notificationPlayer,nil]] ;
};
[_notificationController addChannels:[NSArray
arrayWithObjects:notificationPlayer,nil]];
Run Code Online (Sandbox Code Playgroud)
但是,声音无法播放!我的日志显示以下错误:
2014-08-18 06:21:28.003 MyApp[394:60b] TAAE: Setting audio session category to AVAudioSessionCategoryPlayback
2014-08-18 06:21:28.019 MyApp[394:60b] Couldn't activate audio session: Error Domain=NSOSStatusErrorDomain Code=561015905 "The operation couldn’t be completed. (OSStatus error 561015905.)"
2014-08-18 06:21:28.024 MyApp[394:60b] TAAE: Audio session initialized (audio route '<AVAudioSessionRouteDescription: 0x178006720,
inputs = (null);
outputs = (
"<AVAudioSessionPortDescription: 0x1780067f0, type = Speaker; name = Speaker; UID = Speaker; selectedDataSource = (null)>"
)>')
2014-08-18 06:21:28.034 MyApp[394:60b] 06:21:28.033 ERROR: [0x1938e42a0] >aurioc> 783: failed: '!pla' (enable 2, outf< 2 ch, 44100 Hz, Int16, non-inter> inf< 2 ch, 0 Hz, Float32, non-inter>)
2014-08-18 06:21:28.037 MyApp[394:60b] 06:21:28.037 ERROR: [0x1938e42a0] >aurioc> 783: failed: '!pla' (enable 2, outf< 2 ch, 44100 Hz, Int16, non-inter> inf< 2 ch, 0 Hz, Float32, non-inter>)
Run Code Online (Sandbox Code Playgroud)
在查找!pla错误时,我找到了以下参考:https : //developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html
表示如果应用程序的“信息”属性列表不允许使用音频,或者应用程序在后台且使用的类别不允许背景音频,则可能发生此错误类型。
但是我的plist包含音频作为背景模式,我的音频会话类别为AVAudioSessionCategoryPlayback,类别选项设置为AVAudioSessionCategoryOptionMixWithOthers。
收到远程通知后,如何播放声音?
如果在进入后台之前调用AVAudioSession:setActive:error:并AVAudioSession:setCatagory:withOptions:error:创建AVPlayer ,则可以播放声音。例如:
// Make sure this is run BEFORE entering background.
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if(error != nil){
NSLog(@"ERROR: %@", error.localizedDescription);
}
else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
if(error != nil){
NSLog(@"ERROR: %@", error.localizedDescription);
}
else {
self._player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.urlOfFileToPlay error:&error];
if(error != nil){
[[AVAudioSession sharedInstance] setActive:NO error:&error];
NSLog(@"ERROR: %@", error.localizedDescription);
}
}
}
//You can then do this in application:didReceiveRemoteNotification:fetchCompletionHandler:
[backgroundPlayer.player play];
Run Code Online (Sandbox Code Playgroud)
当然,我假设您已经为后台获取和远程通知设置了后台模式功能。
我还将警告,如果您调用AVAudioSession:setActive:error:得太早,其他应用程序可能会改变您的情况。
另外,如果您将应用程序设置<key>UIApplicationExitsOnSuspend</key>
<true/>为不在后台运行(Info.plist:),则当它收到“ Notification”(本地或远程)通知并且已激活AVAudioSession(如上述代码)时,它将在通知有效负载,无论应用程序是设置为静音还是振动模式,这实际上都是警报的方式。当然,如果您需要轮询服务器以响应远程通知,则此操作将无效。看这里。
| 归档时间: |
|
| 查看次数: |
3098 次 |
| 最近记录: |