xcode 从应用程序播放音频,即使它在后台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20180914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:20:48  来源:igfitidea点击:

Play audio from app, even when it is in the background

xcodeaudiobackground

提问by PanicDev

I am having difficulties determining how I would play audio in the background for my iOS app. I have a NSTimer and wish to have a sound be played when it reaches 5 minutes, even when in the background. I already have the audio background mode enabled, but unsure how to accomplish my idea.

我很难确定如何在我的 iOS 应用程序的后台播放音频。我有一个 NSTimer 并且希望在达到 5 分钟时播放声音,即使是在后台。我已经启用了音频背景模式,但不确定如何实现我的想法。

回答by 4GetFullOf

Under capabilities select the following:

在功能下选择以下内容:

enter image description here

在此处输入图片说明

In your plist make sure where it says "Application does not run in background" is NO.

在您的 plist 中,确保“应用程序不在后台运行”的位置为否。

enter image description here

在此处输入图片说明

Then add the following to your your appDelegate.m file.

然后将以下内容添加到您的 appDelegate.m 文件中。

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

In the following method:

在以下方法中:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}