Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose audio-state-changed on webContents #37366

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
codebytere marked this conversation as resolved.
Show resolved Hide resolved

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

Emitted when media becomes audible or inaudible.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again a little imprecise for the same reason, but ... revising this in a way that still scans well is not obvious to me. For example this would be more correct but feels awkward:

Suggested change
Emitted when media becomes audible or inaudible.
Emitted when the first frame or child content starts playing, or when the last stops playing.

If you have a third idea then let's consider that; otherwise, maybe OK as-is

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the property description but i think this is probably more understandable as-is 🤔


#### 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