2014-12-03 152 views
0

我正在开发闹钟应用程序。在同时报警播放声音,如果应用程序是开放的(前景)的时间则执行ios如何在后台运行闹钟声音

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

方法&报警声成功播放。

但在播放闹钟声时,如果我的应用程序处于不活动/背景状态,则不会调用上述方法。

所以有什么办法可以让我在后台播放闹钟声音。现在,当我的应用程序在后台时,只有通知到达&没有声音/歌曲正在播放。

下面是我的代码

-(void)application:(UIApplication *)application didReceiveLocalNotification(UILocalNotification *)notification 
{ 
     musicPlayer = [MPMusicPlayerController applicationMusicPlayer]; 

     [musicPlayer setQueueWithItemCollection:collection]; 
     [musicPlayer play]; 

     [self showReminder:@"Alarm ON!"]; 
} 

- (void)showReminder:(NSString *)text 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm" 
                 message:text delegate:self 
               cancelButtonTitle:nil 
               otherButtonTitles:@"Snooze",@"Ok",nil]; 
    [alertView show]; 

} 

我只是想知道,当应用程序处于非活动状态/背景也显示警告,停止/暂停闹钟怎么能播放歌曲。

请帮帮我。

+0

你是否得到这个工作? – 2016-02-23 23:22:10

回答

3
0

下面是我的代码,你的mabey试试吧!

- (void)applicationDidEnterBackground:(UIApplication *)application{ 
musicPlayer = [MPMusicPlayerController applicationMusicPlayer]; 

    [musicPlayer setQueueWithItemCollection:collection]; 
    [musicPlayer play]; 

    [self showReminder:@"Alarm ON!"]; 
}