我只需要确认 iPhone 应用程序是否可以在后台播放或流式传输广播。
我创建了一个运行良好的广播应用程序。
我想添加一项功能,用户可以通过该功能在后台进程中播放广播,即他们不必保持应用程序打开即可播放广播。
他们可以关闭应用程序,而 radio 仍在播放。
我很久以前就读到过,Apple 有一个私有(private) API,应用程序可以通过该 API 在后台运行 radio ,但由于它是私有(private)的,所以我无法使用它。
有什么建议吗?
最佳答案
iOS SDK需4.0以上; 首先在您的委托(delegate)代码中:
- (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就可以了;
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:我的英语很差,我希望这对你有帮助:)
关于iphone - 在后台播放广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467567/