Browse Source

Add media play events to webview

Brian R. Bondy 9 years ago
parent
commit
fed0c43970

+ 8 - 0
atom/browser/api/atom_api_web_contents.cc

@@ -452,6 +452,14 @@ void WebContents::PluginCrashed(const base::FilePath& plugin_path,
   Emit("plugin-crashed", info.name, info.version);
 }
 
+void WebContents::MediaStartedPlaying() {
+  Emit("media-started-playing");
+}
+
+void WebContents::MediaPaused() {
+  Emit("media-paused");
+}
+
 void WebContents::DocumentLoadedInFrame(
     content::RenderFrameHost* render_frame_host) {
   if (!render_frame_host->GetParent())

+ 2 - 0
atom/browser/api/atom_api_web_contents.h

@@ -224,6 +224,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
       const std::vector<content::FaviconURL>& urls) override;
   void PluginCrashed(const base::FilePath& plugin_path,
                      base::ProcessId plugin_pid) override;
+  void MediaStartedPlaying() override;
+  void MediaPaused() override;
 
   // brightray::InspectableWebContentsViewDelegate:
   void DevToolsFocused() override;

+ 3 - 1
atom/browser/lib/guest-view-manager.coffee

@@ -22,7 +22,9 @@ supportedWebViewEvents = [
   'page-title-updated'
   'page-favicon-updated'
   'enter-html-full-screen'
-  'leave-html-full-screen'
+  'leave-html-full-screen',
+  'media-started-playing',
+  'media-paused',
 ]
 
 nextInstanceId = 0

+ 2 - 0
atom/renderer/lib/web-view/guest-view-internal.coffee

@@ -20,6 +20,8 @@ WEB_VIEW_EVENTS =
   'crashed': []
   'gpu-crashed': []
   'plugin-crashed': ['name', 'version']
+  'media-started-playing': []
+  'media-paused': []
   'destroyed': []
   'page-title-updated': ['title', 'explicitSet']
   'page-favicon-updated': ['favicons']

+ 8 - 0
docs/api/web-contents.md

@@ -221,6 +221,14 @@ Emitted when `webContents` wants to do basic auth.
 
 The usage is the same with [the `login` event of `app`](app.md#event-login).
 
+### Event: 'media-started-playing'
+
+Emitted when media starts playing.
+
+### Event: 'media-paused'
+
+Emitted when media is paused or done playing.
+
 ## Instance Methods
 
 The `webContents` object has the following instance methods:

+ 3 - 0
spec/fixtures/assets/LICENSE

@@ -0,0 +1,3 @@
+tone.wav
+http://soundbible.com/1815-A-Tone.html
+License: Public Domain

BIN
spec/fixtures/assets/tone.wav


+ 1 - 0
spec/fixtures/pages/audio.html

@@ -0,0 +1 @@
+<audio autoplay muted src="../assets/tone.wav"></audio>

+ 11 - 0
spec/webview-spec.coffee

@@ -379,3 +379,14 @@ describe '<webview> tag', ->
       webview.src = "file://#{fixtures}/pages/onmouseup.html"
       webview.setAttribute 'nodeintegration', 'on'
       document.body.appendChild webview
+
+  describe 'media-started-playing media-paused events', ->
+    it 'emits when audio starts and stops playing', (done) ->
+      audioPlayed = false
+      webview.addEventListener 'media-started-playing', ->
+        audioPlayed = true
+      webview.addEventListener 'media-paused', ->
+        assert audioPlayed
+        done()
+      webview.src = "file://#{fixtures}/pages/audio.html"
+      document.body.appendChild webview