title: How to ignore ringer setting during HTML5 video playback in Cordova
body: <p>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.)</p>
  
  <p>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:</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>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?</p>
  
  <p>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!</p>
  
  <p>Edit: Cordova version in use is 3.5.0-0.2.7</p>
  
  <h2>Current code</h2>
  
  <hr>
  
  <p>Here's the code I've jammed into Cordova's view controller:</p>
  
  <p>I assume that that Cordova's WebView uses the apple default audio setting(<a href="https://developer.apple.com/library/ios/documentation/avfoundation/reference/AVAudioSession_ClassReference/index.html#//apple_ref/doc/constant_group/Audio_Session_Categories" rel="nofollow noreferrer">apple docs</a>) for video playback:</p>
  
  <blockquote>
    <p><strong>AVAudioSessionCategoryAmbient</strong></p>
    
    <p>The category for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.</p>
  </blockquote>
  
  <p>I assume I would like to use the <strong>AVAudioSessionCategoryPlayback</strong> setting.</p>
  
  <p>I edited the main Cordova view controller: </p>
  
  <blockquote>
    <p>'[project/ios-build]/CordovaLib/Classes/CDVViewController.m'</p>
  </blockquote>
  
  <p>by placing this snippet from <a href="https://stackoverflow.com/questions/11616001/uiwebview-html5-audio-pauses-in-ios-6-when-app-enters-background#comment17805618_12414719">another answer</a>  in the "viewDidLoad" handler:</p>
  
  <pre><code>AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  BOOL ok;
  NSError *setCategoryError = nil;
  ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                       error:&amp;setCategoryError];
  if (!ok) {
    NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
  }
  </code></pre>
  
  <p>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. </p>