当收到远程通知时,如何让声音播放?

时间:2021-11-22 07:26:46

I'm trying to automatically play a sound file (that is not part of my app bundle and is not a notification sound) upon receiving a remote notification. I want this to happen whether the app is in the foreground or background when the notification is received.

在收到远程通知时,我正在尝试自动播放一个声音文件(这不是我的应用程序包的一部分,也不是一个通知声音)。我希望在收到通知时,应用程序是否处于前台或后台。

I'm using the Amazing Audio Engine as a wrapper around the core audio libraries. In my App Delegate's didReceiveRemoteNotification I create an Audio Controller and add AEAudioFilePlayer to it like so:

我使用令人惊异的音频引擎作为一个包装器围绕着核心的音频库。在我的应用程序委托的didReceiveRemoteNotification中,我创建了一个音频控制器,并添加了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]];

However, the sound does not play! My logs show the following errors:

然而,声音不播放!我的日志显示了以下错误:

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

Upon looking up the !pla error, I found this reference: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html

在查找!pla错误时,我发现了这个引用:https://developer.apple.com/library/ios/documentation/avaudiosession_classreference/reference/reference/reference/reference/reference.html。

which indicates that this error type can occur if the app’s Information property list does not permit audio use, or if the app is in the background and using a category which does not allow background audio.

这表明如果应用程序的信息属性列表不允许使用音频,或者应用程序在后台并且使用不允许背景音频的类别,就会出现这种错误类型。

But my plist contains the audio as a background mode, my audio session category is AVAudioSessionCategoryPlayback and my category options are set to AVAudioSessionCategoryOptionMixWithOthers.

但是我的plist包含音频作为背景模式,我的音频会话类别是avaudiosessioncategory回放,我的类别选项设置为AVAudioSessionCategoryOptionMixWithOthers。

How do I get my sound to play when a remote notification is received?

当收到远程通知时,如何让声音播放?

2 个解决方案

#1


4  

You cannot activate your audio session in the background, and you cannot initiate sound playback in the background. Background audio simply means that you can keep playing at the time your app goes into the background; but once your background sound has been interrupted, or if your app wasn't playing when you went into the background, you have no ability to make a sound.

您不能在后台激活您的音频会话,并且您不能在后台启动声音播放。背景音频仅仅意味着你可以在应用程序进入后台时继续播放;但是,一旦你的背景声音被打断,或者你的应用程序没有在你进入背景时播放,你就没有能力发出声音。

And this makes sense, because it would be terrible if apps the user isn't using, and may not even be aware of, could start making noise from out of nowhere.

这是有道理的,因为如果用户不使用的应用程序,甚至可能没有意识到的应用程序,可能会从无处不在的地方发出噪音,那就太糟糕了。

The usual thing is either to come to the foreground or to hand the system an immediate local notification that has a sound.

通常情况下,要么来到前台,要么直接向系统发送一个有声音的本地通知。

#2


2  

If you call AVAudioSession:setActive:error: and AVAudioSession:setCatagory:withOptions:error: and create your AVPlayer before going into the background, you can then play your sound. For example:

如果您调用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]; 

Of course I am assuming that you already have the Capabilities for Background Modes set for Background fetch and Remote notifications.

当然,我假设您已经具备了后台获取和远程通知的后台模式。

I will also warn that if you call AVAudioSession:setActive:error: too early, other applications might change things on you.

我还将警告,如果您调用AVAudioSession:setActive:error:太早,其他应用程序可能会对您进行更改。

Also, if you set your application NOT to run in the background (Info.plist: <key>UIApplicationExitsOnSuspend</key> <true/>), when it receives a "Notification" (local or remote) and you have activated an AVAudioSession, like the above code, it will play the sound in the notification payload regardless of if the application is set to silent or vibrate mode, which is actually how alarms do it. Of course this will not work if you need to poll the server in response to a remote notification. See here.

另外,如果您设置了应用程序不运行在后台(Info)。plist: UIApplicationExitsOnSuspend ),当它接收到一个“通知”(本地或远程)时,您已经激活了AVAudioSession,就像上面的代码一样,它将在通知有效负载中播放声音,而不管应用程序是否设置为静音或振动模式,这实际上是警报的作用。当然,如果您需要对服务器进行轮询,以响应远程通知,那么这将不起作用。在这里看到的。

#1


4  

You cannot activate your audio session in the background, and you cannot initiate sound playback in the background. Background audio simply means that you can keep playing at the time your app goes into the background; but once your background sound has been interrupted, or if your app wasn't playing when you went into the background, you have no ability to make a sound.

您不能在后台激活您的音频会话,并且您不能在后台启动声音播放。背景音频仅仅意味着你可以在应用程序进入后台时继续播放;但是,一旦你的背景声音被打断,或者你的应用程序没有在你进入背景时播放,你就没有能力发出声音。

And this makes sense, because it would be terrible if apps the user isn't using, and may not even be aware of, could start making noise from out of nowhere.

这是有道理的,因为如果用户不使用的应用程序,甚至可能没有意识到的应用程序,可能会从无处不在的地方发出噪音,那就太糟糕了。

The usual thing is either to come to the foreground or to hand the system an immediate local notification that has a sound.

通常情况下,要么来到前台,要么直接向系统发送一个有声音的本地通知。

#2


2  

If you call AVAudioSession:setActive:error: and AVAudioSession:setCatagory:withOptions:error: and create your AVPlayer before going into the background, you can then play your sound. For example:

如果您调用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]; 

Of course I am assuming that you already have the Capabilities for Background Modes set for Background fetch and Remote notifications.

当然,我假设您已经具备了后台获取和远程通知的后台模式。

I will also warn that if you call AVAudioSession:setActive:error: too early, other applications might change things on you.

我还将警告,如果您调用AVAudioSession:setActive:error:太早,其他应用程序可能会对您进行更改。

Also, if you set your application NOT to run in the background (Info.plist: <key>UIApplicationExitsOnSuspend</key> <true/>), when it receives a "Notification" (local or remote) and you have activated an AVAudioSession, like the above code, it will play the sound in the notification payload regardless of if the application is set to silent or vibrate mode, which is actually how alarms do it. Of course this will not work if you need to poll the server in response to a remote notification. See here.

另外,如果您设置了应用程序不运行在后台(Info)。plist: UIApplicationExitsOnSuspend ),当它接收到一个“通知”(本地或远程)时,您已经激活了AVAudioSession,就像上面的代码一样,它将在通知有效负载中播放声音,而不管应用程序是否设置为静音或振动模式,这实际上是警报的作用。当然,如果您需要对服务器进行轮询,以响应远程通知,那么这将不起作用。在这里看到的。