Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS7 - UIWebView: HTML5 audio pauses when app enters background

This question is not a duplicated post of this one UIWebView: HTML5 audio pauses in iOS 6 when app enters background

I can access to the track from the control center but the audio is stopped. How avoid the pause state, or how replay the track in the background thread?

All my code is here. You can paste the code in the AppDelegate of a new empty ios app

  • Add AVFoundation Framework
  • Enable Background Audio
  • I's does not work, it's appear in the control center, but music is paused when the app enter in Background!

    #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;
    }
    
like image 635
Damien Romito Avatar asked Jan 27 '14 01:01

Damien Romito


1 Answers

I tried your example code and the url gave a 404. I tried the following url and background mode worked as expected. http://hpr.dogphilosophy.net/test/

When I disabled background audio the background audio paused as expected. I have included the three different ways to enable background audio.

Long story short, you should confirm in your plist that background audio is really enabled. If you still can't get it, post more code.

Enabling background audio via Capabilities

Enabling background audio via plist

Enabling background audio via raw plist

like image 171
Andy Steinmann Avatar answered Sep 30 '22 21:09

Andy Steinmann