Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore ringer setting during HTML5 video playback in Cordova

I would like HTML5 video playback in Cordova to play sound when the ringer switch is set to "mute", like the default behavior in Safari. I managed to get the behavior I would like by fiddling with the Cordova default view controller, but I would rather not modify Cordova's default controllers in place (in part because it seems wrong, but mostly because we'd like to be able to re-build our Cordova app without having to worry about modifying library files.)

So my primary issue is that I'm not quite sure how to proceed with adding some code to Cordova's controller initialization. There are several things I'm uncertain of:

1) Have I missed an obvious setting to have Cordova just do this for me? I see that the Media plugin does something similar to this, but couldn't find anything like it for video.

2) My temporary workaround/proof of concept involves modifying the AudioSession category on the Cordova controller's "viewDidLoad" handler. Am I modifying the controller code at the correct place? Am I clobbering an already-existing session by doing this?

2) If my current code is OK from an Obj-C/ios app initialization standpoint, how can I integrate it with Cordova such that we can build a new Cordova project using it (as this is part of our build process)? If I wrapped the code into a plugin, would "pluginInitialize" be the appropriate place to execute it?

I don't have much in the way of Objective-C/iOS native experience, so if this is mostly a matter of knowing the right things to search for/or understanding app initialization answers to that effect would also be appreciated!

Edit: Cordova version in use is 3.5.0-0.2.7

Current code


Here's the code I've jammed into Cordova's view controller:

I assume that that Cordova's WebView uses the apple default audio setting(apple docs) for video playback:

AVAudioSessionCategoryAmbient

The category for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.

I assume I would like to use the AVAudioSessionCategoryPlayback setting.

I edited the main Cordova view controller:

'[project/ios-build]/CordovaLib/Classes/CDVViewController.m'

by placing this snippet from another answer in the "viewDidLoad" handler:

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

Our videos then played audio with the ringer muted successfully, but, as above I'd like some advice as to how to integrate this code with Cordova, rather than editing in-place.

like image 620
Sterling Nelson Avatar asked Sep 29 '14 20:09

Sterling Nelson


1 Answers

This plugin will do what you want. It basically adds the code you are using and makes your app ignore the mute switch.

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
like image 191
Dev01 Avatar answered Oct 05 '22 17:10

Dev01