1.Info.plist里设置选项Required background modes 添加item0:App plays audio or streams audio/video using AirPlay

2.设置Capabilities -> Background Modes -> 勾选 Audio,AirPlay**

3.在AppDelegate.m中实现
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
4.以上设置只实现了程序后台运行,要通过审核 还需实现app为音频类app。
本文介绍如何在iOS应用中实现音频在后台模式下继续播放的功能。主要步骤包括:在Info.plist文件中设置Requiredbackgroundmodes;启用Capabilities背景模式中的Audio和AirPlay选项;并在AppDelegate.m中实现applicationDidEnterBackground方法来管理背景任务。
2895

被折叠的 条评论
为什么被折叠?



