heysamhey
heysamhey

Reputation: 528

UIWebView: HTML5 audio pauses in iOS 6 when app enters background

Good day,

My app is a music playing app. I control the <audio>-Tag with Javascript. So far no problems, play, pause, next and previous buttons are working. When I stand-by the device in iOS 5, the music keeps playing, but the automatic next song doesn't work. When it isn't in stand-by, it works. And in iOS 6, just after pressing the button, the music fades out.

The Play/Pause button on the lockscreen works in iOS 5, but not in iOS 6.

Upvotes: 27

Views: 21230

Answers (4)

Weles
Weles

Reputation: 1275

Swift Syntax:

in AppDelegate:

import AVFoundation

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  do{
      let audio = AVAudioSession.sharedInstance()
      try audio.setCategory(AVAudioSession.Category.playback)
  }catch let error as NSError{
     print(error)
  }
}

Upvotes: 2

Chris Lundie
Chris Lundie

Reputation: 6023

Starting with iOS 6, you MUST set the audio session category to 'playback' before creating the UIWebView. This is all you have to do. It is not necessary to make the session active.

This should be used for html video as well, because if you don't configure the session, your video will be muted when the ringer switch is off.

#import <AVFoundation/AVFoundation.h>

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                         error:&setCategoryError];
if (!ok) {
  NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}

Ensure that your target links to the AVFoundation framework.


If using Cordova, the file you need to modify is platforms/ios/MyApp/Classes/AppDelegate.m, and will end up looking like this:

#import "AppDelegate.h"
#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    if (!ok) {
        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }

    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

Also, as mentioned in the comments, you need to link the AVFoundation Framework, as explained in this answer:

  • Open your project with xcode open ./platforms/ios/MyApp.xcworkspace/
  • Project navigator > target My App > General
  • Scroll to the bottom to find Linked Frameworks and Libraries

Upvotes: 73

Aaleks
Aaleks

Reputation: 4343

Here the SWIFT 2.0 version to set the audio session category to 'playback' before creating the UIWebView.

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch let error as NSError {
    print(error)  
}

do {
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError {
    print(error)  
}

Upvotes: 0

Dev01
Dev01

Reputation: 14169

This plugin will make your app ignore the mute switch. It's basically the same code that's in the other answers but it's nicely wrapped into a plugin so that you don't have to do any manual objective c edits.

https://github.com/EddyVerbruggen/cordova-plugin-backgroundaudio

Run this command to add it to your project:

cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-backgroundaudio.git

Upvotes: 1

Related Questions