从应用程序播放音频,即使是在后台 [英] Play audio from app, even when it is in the background

查看:187
本文介绍了从应用程序播放音频,即使是在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到困难的决定我怎么会在后台对我的iOS应用程序播放音频。我有一个的NSTimer并希望有当它达到5分钟,声音播放,即使在后台时。我已经启用音频背景模式,但不知道该如何实现我的想法。

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.

推荐答案

在功能选择以下内容:

在你的plist确保它说:应用程序不能在后台运行是NO。

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

然后添加以下到您的appDelegate.m文件。

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

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

在下面的方法:

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

这篇关于从应用程序播放音频,即使是在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆