html 在vaadin中捕获音频标记的timeupdate事件

j8ag8udp  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(60)

我在一个组件中使用了audio标签,如下所示:https://cookbook.vaadin.com/embed-audio
现在我想在音频播放时触发一个事件。我发现audio标签提供了 timeupdate 事件。我可以捕获它,它工作得很好。但是现在我想获取音频播放的当前时间。我读到audio元素有一个属性 currentTime。但是当我想从audio元素获取该属性时,它不存在。
我打印了音频的所有属性,所有的属性都是 srcstyle。希望你能在这里给我指出正确的方向,因为这对我来说绝对没有意义。
编辑:当我通过executeJs查询属性时,如下所示:

UI.getCurrent().getPage().executeJs("return $0.currentTime", audio.getElement());

字符串
.,我得到了我正在寻找的值,但随后,事件侦听器不再同步工作。
我的类看起来是这样的:

@Tag("audio")
public class Audio extends Component implements HasSize {

    private static final long serialVersionUID = 1L;

    private static final PropertyDescriptor<String, String> srcDescriptor = PropertyDescriptors
            .attributeWithDefault("src", "");

    public Audio() {
        super();
        getElement().setProperty("controls", true);
    }

    public Audio(String src) {
        setSrc(src);
        getElement().setProperty("controls", true);
    }

    public String getSrc() {
        return get(srcDescriptor);
    }

    public void setSrc(String src) {
        set(srcDescriptor, src);
    }

    public void setSrc(final AbstractStreamResource resource) {
        getElement().setAttribute("src", resource);
    }

    public void play() {
        getElement().callJsFunction("play");
    }

    public void stop() {
        getElement().callJsFunction("stop");
    }

    @DomEvent("timeupdate")
    public static class TimeUpdateEvent extends ComponentEvent<Audio> {

        private final int progress;

        public TimeUpdateEvent(Audio audio, boolean fromClient) {
            super(audio, fromClient);
            String currentTime = audio.getElement().getAttribute("currentTime");
            this.progress = Integer.valueOf(currentTime);
        }

        public Audio getAudio() {
            return (Audio) this.source;
        }

        public int getProgress() {
            return this.progress;
        }
    }

    public Registration addTimeUpdateListener(ComponentEventListener<Audio.TimeUpdateEvent> listener) {
        return addListener(Audio.TimeUpdateEvent.class, listener);
    }
}

ff29svar

ff29svar1#

好的,我找到了一种使用属性chance侦听器的方法。我观察JavaScript属性 currentTime 并对 timeupdate 事件做出React。这不会发出 * timeupdate * 事件,但我可以接受。

public Audio() {
    super();
    this.getElement().addPropertyChangeListener("currentTime", "timeupdate", e -> {
        currentTime = Float.parseFloat(e.getValue().toString());
    });
    getElement().setProperty("controls", true);
}

字符串

相关问题