ios - 收到远程通知时如何让我的声音播放?

标签 ios objective-c ios7 core-audio avaudioplayer

我正在尝试在收到远程通知时自动播放声音文件(它不是我的应用程序包的一部分,也不是通知声音)。当收到通知时,无论应用程序是在前台还是后台,我都希望发生这种情况。

我使用 Amazing Audio Engine 作为核心音频库的包装器。在我的 App Delegate 的 didReceiveRemoteNotification 中,我创建了一个音频 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]];

但是,声音不播放!我的日志显示以下错误:

    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>)

在查找 !pla 错误时,我找到了这个引用:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html

这表示如果应用的信息属性列表不允许使用音频,或者如果应用在后台并且使用不允许背景音频的类别,则可能会发生此错误类型。

但我的 plist 包含音频作为背景模式,我的 Audio Session 类别是 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]; 

当然,我假设您已经为后台获取和远程通知设置了后台模式功能。

如果您调用 AVAudioSession:setActive:error:,我也会发出警告为时过早,其他应用程序可能会改变您的情况。

此外,如果您将应用程序设置为不在后台运行(Info.plist:<key>UIApplicationExitsOnSuspend</key> <true/>),当它收到“通知”(本地或远程)并且您已激活 AVAudioSession 时,如上面的代码,无论应用程序设置为静音模式还是振动模式,它都会在通知有效负载中播放声音,这实际上警报是如何做到的。当然,如果您需要轮询服务器以响应远程通知,这将不起作用。 See here.

关于ios - 收到远程通知时如何让我的声音播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25344403/

相关文章:

ios - 反射(reflect)绘制后UIView的变化

ios - if 语句 (CGAffineTransform) 不起作用

ios - 知道两个CGRect的交点

ios - 从 OpenGL 绘图创建 png UIImage

ios - 从另一个类更新 UI

ios - 按按钮打开 Safari,如何返回我的应用程序

ios - 我可以在同一个 tableViewCell 中使用 IBOutlet 和 IBAction 吗?

objective-c - 使用 Storyboard 通过滑动更改 View

ios7 - 搜索栏同时显示 "e"和 "é"的结果

ios - 如何使用 spritekit 使背景连续垂直滚动