Skip to content

Commit

Permalink
feat: expose audio-state-changed on webContents (#37366)
Browse files Browse the repository at this point in the history
feat: expose audio-state-changed on webContents
  • Loading branch information
codebytere committed Mar 6, 2023
1 parent c8f715f commit 512e56b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 9 additions & 0 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,15 @@ Emitted when media starts playing.

Emitted when media is paused or done playing.

#### Event: 'audio-state-changed'

Returns:

* `event` Event<>
* `audible` boolean - True if one or more frames or child `webContents` are emitting audio.

Emitted when media becomes audible or inaudible.

#### Event: 'did-change-theme-color'

Returns:
Expand Down
9 changes: 8 additions & 1 deletion shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,14 @@ content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
}

void WebContents::OnAudioStateChanged(bool audible) {
Emit("-audio-state-changed", audible);
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<gin_helper::internal::Event> event =
gin_helper::internal::Event::New(isolate);
v8::Local<v8::Object> event_object = event.ToV8().As<v8::Object>();
gin::Dictionary dict(isolate, event_object);
dict.Set("audible", audible);
EmitWithoutEvent("audio-state-changed", event);
}

void WebContents::BeforeUnloadFired(bool proceed,
Expand Down
4 changes: 2 additions & 2 deletions spec/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@ describe('webContents module', () => {
oscillator.connect(context.destination)
oscillator.start()
`);
let p = once(w.webContents, '-audio-state-changed');
let p = once(w.webContents, 'audio-state-changed');
w.webContents.executeJavaScript('context.resume()');
await p;
expect(w.webContents.isCurrentlyAudible()).to.be.true();
p = once(w.webContents, '-audio-state-changed');
p = once(w.webContents, 'audio-state-changed');
w.webContents.executeJavaScript('oscillator.stop()');
await p;
expect(w.webContents.isCurrentlyAudible()).to.be.false();
Expand Down

0 comments on commit 512e56b

Please sign in to comment.