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

Adds autoplay and muted to media #3053

Merged
merged 2 commits into from
Jan 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion examples/reference/panes/Audio.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"* **``loop``** (boolean): Whether to loop when reaching the end of playback\n",
"* **``object``** (string): Local file path or remote URL pointing to audio file\n",
"* **``paused``** (boolean): Whether the player is paused\n",
"* **``autoplay``** (boolean): When True, it specifies that the output will play automatically. In Chromium browsers this requires the user to click play once\n",
"* **``muted``** (boolean): When True, it specifies that the output should be muted\n",
"* **``throttle``** (int): How frequently to sample the current playback time in milliseconds\n",
"* **``time``** (float): Current playback time in seconds\n",
"* **``volume``** (int): Volume in the range 0-100\n",
Expand Down Expand Up @@ -150,9 +152,22 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"pygments_lexer": "ipython3"
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
17 changes: 16 additions & 1 deletion examples/reference/panes/Video.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"* **``loop``** (boolean): Whether to loop when reaching the end of playback\n",
"* **``object``** (string): Local file path or remote URL pointing to audio file\n",
"* **``paused``** (boolean): Whether the player is paused\n",
"* **``autoplay``** (boolean): When True, it specifies that the output will play automatically. In Chromium browsers this requires the user to click play once\n",
"* **``muted``** (boolean): When True, it specifies that the output should be muted\n",
"* **``throttle``** (int): How frequently to sample the current playback time in milliseconds\n",
"* **``time``** (float): Current playback time in seconds\n",
"* **``volume``** (int): Volume in the range 0-100\n",
Expand Down Expand Up @@ -118,9 +120,22 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"pygments_lexer": "ipython3"
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
16 changes: 16 additions & 0 deletions panel/models/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class AudioView extends PanelHTMLBoxView {
this.connect(this.model.properties.time.change, () => this.set_time())
this.connect(this.model.properties.value.change, () => this.set_value())
this.connect(this.model.properties.volume.change, () => this.set_volume())
this.connect(this.model.properties.muted.change, () => this.set_muted())
this.connect(this.model.properties.autoplay.change, () => this.set_autoplay())
}

render(): void {
Expand All @@ -34,6 +36,8 @@ export class AudioView extends PanelHTMLBoxView {
this.audioEl.src = this.model.value
this.audioEl.currentTime = this.model.time
this.audioEl.loop = this.model.loop
this.audioEl.muted = this.model.muted
this.audioEl.autoplay = this.model.autoplay
if (this.model.volume != null)
this.audioEl.volume = this.model.volume/100
else
Expand Down Expand Up @@ -72,6 +76,14 @@ export class AudioView extends PanelHTMLBoxView {
this.audioEl.loop = this.model.loop
}

set_muted(): void {
this.audioEl.muted = this.model.muted
}

set_autoplay(): void {
this.audioEl.autoplay = this.model.autoplay
}

set_paused(): void {
if (!this.audioEl.paused && this.model.paused)
this.audioEl.pause()
Expand Down Expand Up @@ -109,6 +121,8 @@ export namespace Audio {
export type Props = HTMLBox.Props & {
loop: p.Property<boolean>
paused: p.Property<boolean>
muted: p.Property<boolean>
autoplay: p.Property<boolean>
time: p.Property<number>
throttle: p.Property<number>
value: p.Property<any>
Expand All @@ -133,6 +147,8 @@ export class Audio extends HTMLBox {
this.define<Audio.Props>(({Any, Boolean, Int, Number}) => ({
loop: [ Boolean, false ],
paused: [ Boolean, true ],
muted: [ Boolean, false ],
autoplay: [ Boolean, false ],
time: [ Number, 0 ],
throttle: [ Number, 250 ],
value: [ Any, '' ],
Expand Down
16 changes: 16 additions & 0 deletions panel/models/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class VideoView extends PanelHTMLBoxView {
super.connect_signals()
this.connect(this.model.properties.loop.change, () => this.set_loop())
this.connect(this.model.properties.paused.change, () => this.set_paused())
this.connect(this.model.properties.muted.change, () => this.set_muted())
this.connect(this.model.properties.autoplay.change, () => this.set_autoplay())
this.connect(this.model.properties.time.change, () => this.set_time())
this.connect(this.model.properties.value.change, () => this.set_value())
this.connect(this.model.properties.volume.change, () => this.set_volume())
Expand All @@ -43,6 +45,8 @@ export class VideoView extends PanelHTMLBoxView {
this.videoEl.src = this.model.value
this.videoEl.currentTime = this.model.time
this.videoEl.loop = this.model.loop
this.videoEl.muted = this.model.muted
this.videoEl.autoplay = this.model.autoplay
if (this.model.volume != null)
this.videoEl.volume = this.model.volume/100
else
Expand Down Expand Up @@ -81,6 +85,14 @@ export class VideoView extends PanelHTMLBoxView {
this.videoEl.loop = this.model.loop
}

set_muted(): void {
this.videoEl.muted = this.model.muted
}

set_autoplay(): void {
this.videoEl.autoplay = this.model.autoplay
}

set_paused(): void {
if (!this.videoEl.paused && this.model.paused)
this.videoEl.pause()
Expand Down Expand Up @@ -117,6 +129,8 @@ export namespace Video {
export type Props = HTMLBox.Props & {
loop: p.Property<boolean>
paused: p.Property<boolean>
muted: p.Property<boolean>
autoplay: p.Property<boolean>
time: p.Property<number>
throttle: p.Property<number>
value: p.Property<any>
Expand All @@ -141,6 +155,8 @@ export class Video extends HTMLBox {
this.define<Video.Props>(({Any, Boolean, Int, Number}) => ({
loop: [ Boolean, false ],
paused: [ Boolean, true ],
muted: [ Boolean, false ],
autoplay: [ Boolean, false ],
time: [ Number, 0 ],
throttle: [ Int, 250 ],
value: [ Any, '' ],
Expand Down
8 changes: 8 additions & 0 deletions panel/models/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class Audio(HTMLBox):

paused = Bool(False, help="""Whether the audio is paused""")

muted = Bool(False, help="""Whether the audio is muted""")

autoplay = Bool(False, help="""Whether the audio is playing automatically""")

time = Float(0, help="""
The current time stamp of the audio playback""")

Expand All @@ -84,6 +88,10 @@ class Video(HTMLBox):

paused = Bool(False, help="""Whether the video is paused""")

muted = Bool(False, help="""Whether the video is muted""")

autoplay = Bool(False, help="""Whether the video is playing automatically""")

time = Float(0, help="""
The current time stamp of the video playback""")

Expand Down
7 changes: 7 additions & 0 deletions panel/pane/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class _MediaBase(PaneBase):
volume = param.Number(default=None, bounds=(0, 100), doc="""
The volume of the media player.""")

autoplay = param.Boolean(default=False, doc="""
When True, it specifies that the output will play automatically.
In Chromium browsers this requires the user to click play once.""")

muted = param.Boolean(default=False, doc="""
When True, it specifies that the output should be muted.""")
Copy link
Member

Choose a reason for hiding this comment

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

Parameter definitions should be alphabetically sorted.


_default_mime = None

_formats = []
Expand Down
27 changes: 27 additions & 0 deletions panel/tests/pane/test_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def test_local_video(document, comm):
assert model.value == 'data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAAhtZGF0AAAA1m1vb3YAAABsbXZoZAAAAAAAAAAAAAAAAAAAA+gAAAAAAAEAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAABidWR0YQAAAFptZXRhAAAAAAAAACFoZGxyAAAAAAAAAABtZGlyYXBwbAAAAAAAAAAAAAAAAC1pbHN0AAAAJal0b28AAAAdZGF0YQAAAAEAAAAATGF2ZjU3LjQxLjEwMA==' # noqa


def test_video_autoplay(document, comm):
video = Video(str(ASSETS / 'mp4.mp4'), autoplay=True)
model = video.get_root(document, comm=comm)

assert model.autoplay

def test_video_muted(document, comm):
video = Video(str(ASSETS / 'mp4.mp4'), muted=True)
model = video.get_root(document, comm=comm)

assert model.muted


def test_local_audio(document, comm):
audio = Audio(str(ASSETS / 'mp3.mp3'))
model = audio.get_root(document, comm=comm)
Expand Down Expand Up @@ -86,3 +99,17 @@ def test_audio_url(document, comm):
model = audio.get_root(document, comm=comm)

assert audio_url == model.value

def test_audio_muted(document, comm):
audio_url = 'http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3'
audio = Audio(audio_url, muted=True)
model = audio.get_root(document, comm=comm)

assert model.muted

def test_audio_autoplay(document, comm):
audio_url = 'http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3'
audio = Audio(audio_url, autoplay=True)
model = audio.get_root(document, comm=comm)

assert model.autoplay