2012-03-02 75 views
1

我想在特定的時間播放音頻文件,我已經set.for,我已經在info.plist中的「UIBackgroundModes」和didEnterBackground.Its中的backgroundtaskidentifier添加音頻工作在模擬器,但不是在device.Can任何人都可以幫助解決此問題。如何在iPhone中播放背景音頻?

在此先感謝...

+0

看到這個http://stackoverflow.com/questions/7619794/play-music-in-background-in-iphone-using-avaudioplayer/7619816#7619816 – 2012-03-09 07:14:02

回答

1

你可以看看這個reference app with streaming and backgrounding of audio

編輯:

一個本地通知瓦特/附加的聲音的實施例。 More detail here

- (void)applicationDidEnterBackground:(UIApplication *)application { 
    NSLog(@"Application entered background state."); 
    // bgTask is instance variable 
    NSAssert(self->bgTask == UIInvalidBackgroundTask, nil); 

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [application endBackgroundTask:self->bgTask]; 
      self->bgTask = UIInvalidBackgroundTask; 
     }); 
    }]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     while ([application backgroundTimeRemaining] > 1.0) { 
      NSString *friend = [self checkForIncomingChat]; 
      if (friend) { 
       UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
       if (localNotif) { 
        localNotif.alertBody = [NSString stringWithFormat: 
         NSLocalizedString(@"%@ has a message for you.", nil), friend]; 
        localNotif.alertAction = NSLocalizedString(@"Read Message", nil); 
        localNotif.soundName = @"alarmsound.caf"; 
        localNotif.applicationIconBadgeNumber = 1; 
        [application presentLocalNotificationNow:localNotif]; 
        [localNotif release]; 
        friend = nil; 
        break; 
       } 
      } 
     } 
     [application endBackgroundTask:self->bgTask]; 
     self->bgTask = UIInvalidBackgroundTask; 
    }); 

} 
+0

它的工作原理,如果我們進入後臺,而音頻播放,但我想要在輸入應用程序後開始播放音頻到backgrouond中。任何人都可以打電話給我該怎麼做 – Ann 2012-03-03 09:42:33

+0

用戶通過雙擊主屏幕按鈕並向右滑動或從鎖定屏幕手動啓動音頻控制。或者,你安排一個聲音剪輯的本地通知,而不是音頻流 – slf 2012-03-04 16:46:57

+0

謝謝他的工作很好..... – Ann 2012-03-05 05:48:49

1

如果我理解正確的話,你要在特定時間播放聲音,無論你的應用程序是活動的,當時前景化?您唯一的(應用程序審查友好)選項是安排本地通知,如slf的答案。 UIBackgroundModes不會幫助你:音頻背景模式僅用於當你預先播放音頻時開始播放時允許執行代碼,而後臺播放。示例用法是音樂播放器和廣播應用程序。

如果您更徹底地解釋您的用例,我可能會有更好的答案。

相關問題