仅在播放音频时才在后台运行应用程序

时间:2012-11-29 10:46:10

标签: iphone objective-c ios ipad

是否可以在播放音频时在后台运行应用程序?

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

要做到这一点,我已经在info.plist中设置了

Application does not run in background          YES

然后音频不在后台播放。

现在运行音频我设置

if(AUDIO_IS_PLAYING==NO){

    exit(0);

} 

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

如果你知道只在音频正在播放的情况下在后台运行应用程序,那么任何其他方式都将被关闭。

1 个答案:

答案 0 :(得分: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
}
相关问题