2011-03-29 82 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]; 
}];