【问题标题】:Audio feed back in foreground and background前台和后台的音频反馈
【发布时间】:2023-04-02 19:58:01
【问题描述】:

当我的应用程序在前台和后台时,我必须提供音频反馈。但是当应用程序进入后台时,听不到音频反馈。在 info.plist 中,我已将后台模式设置为 App 使用 AirPlay 播放音频或流式传输音频/视频并使用以下内容,但当应用程序进入后台且听不到音频时,不会调用 audioPlayerDidFinishPlaying 委托。

 AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
        sound.delegate = self;
        ///Fixed the issue No audible feedback when main audio is silent
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance] setActive: YES error: nil];
        // This is necessary if you want to play a sequence of songs, otherwise your app will be
        // killed after the first one finishes.
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

        [soundQueue addObject:sound];

【问题讨论】:

标签: ios iphone objective-c avaudioplayer


【解决方案1】:

您需要在 plist 文件中进行一些更改。

1) 将必需的后台模式设置为应用播放音频

2) 设置应用程序不在后台运行为YES。

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

那么,你需要在 AppDelegate 中写这么多代码

现在,您可以在手机屏幕锁定或在后台轻松播放音频。

它对我来说很好用。 :)

更多信息请参考Playing Audio in background modeAudio Session Programming Guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多