2011-03-29 141 views
1

我只需要確認一個iPhone應用程序是否可以在後臺播放或流式播放電臺。在後臺播放電臺

我創建了一個工作正常的廣播應用程序。

我想添加一個功能,用戶可以在後臺進程中播放收音機,即他們不必讓應用程序打開播放收音機。

他們可以關閉應用程序,收音機仍在播放。

我讀了很久,蘋果有一個私有的API,應用程序可以在後臺運行無線電,但由於它是私人的,我無法使用它。

有什麼建議嗎?

+0

我不知道如何實現它,但是,是的,你可以玩音樂,而你的應用程序在後臺。它是iOS支持的後臺進程之一。 – Robin 2011-03-29 03:38:37

回答

4

iOS SDK應該高於4.0; 先在委託代碼:

- (void)applicationDidEnterBackground:(UIApplication *)application { 

    if (isAbove4_0) 

{ 

     CTCallCenter *_center = [[CTCallCenter alloc] init]; 

     _center.callEventHandler = ^(CTCall *call) 
     { 

      //NSLog(@"call:%@", call.callState); 
      if ([call.callState isEqualToString:CTCallStateIncoming]) 

       { 

       [RadioPlayer pausePlayer]; 

     }   
    }; 
    } 

} 

然後, 我用的MPMoviePlayerController,並AVPlayer是OK;

if (isAbove4_0) { 
    if (_mpmovieplayer == nil) { 
     _mpmovieplayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
     [_mpmovieplayer setMovieControlMode:MPMovieControlModeHidden]; 
    } 

    [_mpmovieplayer setContentURL:url]; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
    // This is necessary if you want to play a sequence of songs, otherwise your app will be 
    // killed after the first one finishes. 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; 

    [_mpmovieplayer play]; 

    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; 
    if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid) 
     [[UIApplication sharedApplication] endBackgroundTask: bgTaskId]; 
    bgTaskId = newTaskId; 

} 

PS:我的英語很差,我希望這能對您有所幫助:)

+0

不用擔心英文,哥們,我會試試 – iscavengers 2011-03-29 11:01:22

0

這是它對我的作品。

添加以下到您的應用程序,Info.plist文件

<key>UIBackgroundModes</key> 
<array> 
<string>audio</string> 
</array> 

然後在應用中:didFinishLaunchingWithOptions增加下面

UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{ 
    /* just fail if this happens. */ 
    NSLog(@"BackgroundTask Expiration Handler is called"); 
    [application endBackgroundTask:backgroundTask]; 
}];