1

只有在播放音频时才可以在后台运行应用程序吗?

我制作了一个应用程序,我希望当应用程序进入后台时,它会检查音频是否正在播放。如果正在播放音频,则应用程序将在后台运行,否则应用程序将关闭。

为此,我已在 info.plist 中设置

Application does not run in background          YES

然后音频不在后台播放。

现在运行我设置的音频

if(AUDIO_IS_PLAYING==NO){

    exit(0);

} 

但我认为苹果不会允许这样做。

如果您知道仅在播放音频时才在后台运行应用程序,则任何其他方式将被关闭。

4

1 回答 1

2

使用通知:

当应用程序进入后台时会发布以下通知。

UIApplicationDidEnterBackgroundNotification

当应用程序变为活动状态时,会发布以下通知。

UIApplicationDidBecomeActiveNotification

在函数中添加通知观察者

//Adding observer for notification
-(void)viewDidLoad
{
    // Your other code goes here

    // Adding observer for notification when application entered the background
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

// This method will be called when application entered in background
-(void)applicationDidEnterBackgroundNotification:(id)sender
{
    // Do whatever you want when application in background
}
于 2012-11-29T13:26:31.637 回答