UIWebView: HTML5 audio pauses in iOS 6 when app enters background
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
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
@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
iOS webview and audio html5 on background
From http://www.raywenderlich.com/29948/backgrounding-for-ios, the part which explains Playing Audio in Background
: "If it still doesn’t work for you, it is probably because you’re using the Simulator. Testing it on a device should get it working"
You should try on the Device.
Related Topics
Do Something Every X Minutes in Swift
How to Put Buttons Over Uitableview Which Won't Scroll With Table in Ios
Get a List of All Contacts on Ios
How to Add Image and Text in Uitextview in Ios
Are There APIs For Custom Vibrations in Ios
Uiviewcontroller Viewdidload Vs. Viewwillappear: What Is the Proper Division of Labor
iOS (Iphone, iPad, Ipodtouch) View Real-Time Console Log Terminal
Convert Array to JSON String in Swift
How to Implement Re-Ordering of Coredata Records
iPad Safari - Make Keyboard Disappear
Swift Get Request With Parameters
Create Uiimage With Solid Color in Swift
Storyboard Doesn't Contain a View Controller With Identifier
Enterprise App Update Distribution on iOS 8
Customize Uitableview Header Section
How to Create a Swift Date Object