这个问题不是这个问题的重复帖子 UIWebView: HTML5 audio pauses in iOS 6 when app enters background
我可以从控制中心访问轨道,但音频已停止。 如何避免暂停状态,或者如何在后台线程中重放轨道?
我所有的代码都在这里。您可以将代码粘贴到一个新的空 ios 应用程序的 AppDelegate 中
- 添加 AVFoundation 框架
- 启用背景音频
我的不工作,它出现在控制中心,但当应用程序进入后台时音乐暂停!
#import "AppDelegate.h" #import <AVFoundation/AVFoundation.h> @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. AVAudioSession *audioSession = [AVAudioSession sharedInstance]; BOOL ok; NSError *setCategoryError = nil; ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; if (!ok) { NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError); } UIViewController *vc = [[UIViewController alloc] init]; UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController: vc]; self.window.rootViewController = nav; //WEBVIEW UIWebView *myWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 200, 320, vc.view.frame.size.height)]; myWeb.mediaPlaybackRequiresUserAction = NO; myWeb.allowsInlineMediaPlayback = YES; [vc.view addSubview:myWeb]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.romito.fr/public/inlineHTML5/"]]; [myWeb loadRequest: request]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
最佳答案
我尝试了您的示例代码,url 给出了 404。我尝试了以下 url,后台模式按预期工作。 http://hpr.dogphilosophy.net/test/
当我禁用背景音频时,背景音频按预期暂停。我已经包含了三种不同的方式来启用背景音频。
长话短说,您应该在 plist 中确认背景音频确实已启用。如果还是无法获取,请发布更多代码。
关于IOS7-UIWebView : HTML5 audio pauses when app enters background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21371475/