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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement autoplay for Video and Audio #4453

Merged
merged 6 commits into from Jun 8, 2023
Merged

implement autoplay for Video and Audio #4453

merged 6 commits into from Jun 8, 2023

Conversation

pngwn
Copy link
Member

@pngwn pngwn commented Jun 8, 2023

Description

Closes #1349.

Implements autoplay kwarg for Audio and Video.

Can test with this:

import gradio as gr

def do_audio(audio):
    return audio


with gr.Blocks() as demo:
    a1 = gr.Video(autoplay=True)
    a2 = gr.Video(autoplay=True)
    btn = gr.Button("do thing")

    btn.click(do_audio, inputs=a1, outputs=a2)


demo.launch()

This will work in most cases but it won't work on page load because browsers disallow it. There are workaround (although they are pretty messy) but it is obnoxious anyway, so i think it is fine that it doesn't work.

Please include:

  • relevant motivation
  • a summary of the change
  • which issue is fixed.
  • any additional dependencies that are required for this change.

Closes: # (issue)

Checklist:

  • I have performed a self-review of my own code
  • I have added a short summary of my change to the CHANGELOG.md
  • My code follows the style guidelines of this project
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

A note about the CHANGELOG

Hello 馃憢 and thank you for contributing to Gradio!

All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.

Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by [@myusername](link-to-your-github-profile) in [PR 11111](https://github.com/gradio-app/gradio/pull/11111)".

If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.

Comment on lines +36 to +48
let el: HTMLAudioElement;

let old_val: any;
function value_has_changed(val: any) {
if (val === old_val) return false;
else {
old_val = val;
return true;
}
}

$: autoplay && el && value_has_changed(value) && el.play();

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 don't love the duplication but its ok for now, we should create a static AudioPlayer like we have withe Video to abstract this at some point.

Comment on lines -21 to 22
let show_controls = true;
let show_controls_timeout: NodeJS.Timeout;

function video_move() {
clearTimeout(show_controls_timeout);
show_controls_timeout = setTimeout(() => (show_controls = false), 500);
show_controls = true;
}

function handleMove(e: TouchEvent | MouseEvent) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is all just unused code from a previous change.

@gradio-pr-bot
Copy link
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-4453-all-demos

@pngwn pngwn changed the title implement autoplay implement autoplay for Video and Audio Jun 8, 2023
Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

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

LGTM!

@pngwn pngwn merged commit 3d334e2 into main Jun 8, 2023
17 of 19 checks passed
@pngwn pngwn deleted the 1349-autoplay branch June 8, 2023 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option for autoplay in Audio component
3 participants