Skip to content

Add current_time attribute to Audio widget #3665

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/controls/src/widget_audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class AudioModel extends CoreDOMWidgetModel {
autoplay: true,
loop: true,
controls: true,
current_time: 0,
value: new DataView(new ArrayBuffer(0)),
};
}
Expand Down Expand Up @@ -70,6 +71,7 @@ export class AudioView extends DOMWidgetView {
this.el.loop = this.model.get('loop');
this.el.autoplay = this.model.get('autoplay');
this.el.controls = this.model.get('controls');
this.el.currentTime = this.model.get('current_time');

return super.update();
}
Expand Down
21 changes: 18 additions & 3 deletions python/ipywidgets/ipywidgets/widgets/widget_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import mimetypes

from .widget_core import CoreWidget
from traitlets import Bool, CUnicode, Int, TraitError, Unicode, validate

from .domwidget import DOMWidget
from .trait_types import CByteMemoryView
from .valuewidget import ValueWidget
from .widget import register
from traitlets import Unicode, CUnicode, Bool
from .trait_types import CByteMemoryView
from .widget_core import CoreWidget


@register
Expand Down Expand Up @@ -215,10 +216,24 @@ class Audio(_Media):
autoplay = Bool(True, help="When true, the audio starts when it's displayed").tag(sync=True)
loop = Bool(True, help="When true, the audio will start from the beginning after finishing").tag(sync=True)
controls = Bool(True, help="Specifies that audio controls should be displayed (such as a play/pause button etc)").tag(sync=True)
current_time = Int(
allow_none=True,
help="The current_time property sets or returns the current position (in seconds) of the audio/video playback."
).tag(sync=True)

@classmethod
def from_file(cls, filename, **kwargs):
return cls._from_file('audio', filename, **kwargs)

def __repr__(self):
return self._get_repr(Audio)

@validate("current_time")
def _validate_current_time(self, proposal):
"""Validate that current_time >= 0.
"""
current_time = proposal["value"] or 0
if current_time < 0:
raise TraitError("Current time property must be greater than 0.")

return current_time