当应用程序在后台播放音频

时间:2019-07-05 07:58:17

标签: ios objective-c

我已经阅读了SO中的其他线程,但似乎没有一个适合我的情况。

我的应用程序在收到推送通知时将播放音频文件。 如果我的应用程序在前台,那就很好。但是当我按下锁定按钮使手机进入睡眠状态时。

[audioSession setCategory:AVAudioSessionCategoryPlayback
                                         mode:AVAudioSessionModeDefault
                           routeSharingPolicy:AVAudioSessionRouteSharingPolicyDefault
                                      options:AVAudioSessionCategoryOptionMixWithOthers error:&error];
                    NSLog(@"###########START set category with error: %@",error.description);
                    [audioSession setActive:true error:&error];
                    NSLog(@"###########START set active with error: %@",error.description);

当我尝试激活音频会话时显示错误。

###########START set category with error: (null) 2019-07-05 15:51:35.149386+0800 [1936:804557] 
###########START set active with error: Error Domain=NSOSStatusErrorDomain Code=561015905 "(null)"

错误561015905是AVAudioSessionErrorCodeCannotStartPlaying

来自Apple文档:

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

但是我确实在“功能”中启用了后台音频,并且还允许使用音频。

我的类别设置有问题吗?

我检查了控制台日志。我知道了

default 17:10:19.487601 +0800   mediaserverd    1242: { "action":"set_category", "session":{"ID":"0x1ac7d","PID":2181,"name":"MY_APP"}, "details":{"key":"AudioCategory","value":"MediaPlayback"} }
default 17:10:19.489471 +0800   mediaserverd    2832: { "action":"set_property", "session":{"ID":"0x1ac7d","PID":2181,"name":"MY_APP"}, "details":{"key":"InterruptionStyle","value":"Mix with others"} }
default 17:10:19.491276 +0800   mediaserverd    2832: { "action":"set_property", "session":{"ID":"0x1ac7d","PID":2181,"name":"MY_APP"}, "details":{"key":"WantsToPauseSpokenAudio","value":false} }
default 17:10:19.494890 +0800   mediaserverd    2919: { "action":"activate", "session":{"ID":"0x1ac7d","PID":2181,"name":"MY_APP"}, "details":"entry" }
default 17:10:19.501012 +0800   mediaserverd    -CMSUtilities- CMSUtility_IsAllowedToStartPlaying: CMSession: Client sid:0x1ac7d, MY_APP(2181), 'prim' with category MediaPlayback and mode Default and mixable does not have assertions to start mixable playback
default 17:10:19.501838 +0800   mediaserverd    -CMSessionMgr- _CMSessionBeginInterruption_WithFlags: CMSession: CMSessionBeginInterruption failed as client 'sid:0x1ac7d, MY_APP(2181), 'prim'' has insufficient privileges to take control
error   17:10:19.503217 +0800   mediaserverd    259: { "action":"cm_session_begin_interruption", "error":"translating CM session error", "session":{"ID":"0x1ac7d","PID":2181,"name":"MY_APP"}, "details":{"calling_line":2949,"error_code":-16980,"error_string":"Operation denied. Cannot start playing"} }

1 个答案:

答案 0 :(得分:0)

我发现了问题,我应该用VoIP通知而不是正常的推送通知来唤醒手机