Sunday, November 6, 2016

Re: Bug in Audio addEndedHandler

I'm guessing this is intended functionality, and not a bug (apologies for thinking it was a bug), as Chrome, Firefox, and Edge all only fire the event if the Audio element is actually added to the DOM.


On Sunday, 6 November 2016 21:06:28 UTC+11, Craig Mitchell wrote:
Thanks Thomas.

I'm using Chrome.  However, the difference is, I wasn't adding the Audio to the page.  Ie: Not calling RootPanel.get().add(audio);

So, in this test:

public void onModuleLoad() {
Audio audio = Audio.createIfSupported();
audio.setControls(true);
audio.setAutoplay(true);

audio.addEndedHandler(new EndedHandler() {
@Override
public void onEnded(EndedEvent event) {
Window.alert("Ended with event");
}
});
listenForEnd(audio.getAudioElement(), new Command() {
@Override
public void execute() {
Window.alert("Ended with handler");
}
});
RootPanel.get().add(new InlineHTML("Hello world"));
}

public static native final void listenForEnd(AudioElement aud, Command onComplete) /*-{
   aud.onended = function() {
      onComplete.@com.google.gwt.user.client.Command::execute(*)();
   };
}-*/;

The Audio still plays, however, only the "Ended with handler" will be called, and not "Ended with event".

Cheers.


On Sunday, 6 November 2016 20:50:10 UTC+11, Thomas Broyer wrote:


On Sunday, November 6, 2016 at 6:10:08 AM UTC+1, Craig Mitchell wrote:
If you want to listen for the end of an audio track, calling MediaBase.addEndedHandler won't work, as it is using BrowserEvents.ENDED which is set to "ended".

The correct event is actually "onended". Ref:  http://www.w3schools.com/tags/av_event_ended.asp

Wrong.
The event name is "ended", the "event handler" name is "onended" (see https://html.spec.whatwg.org/multipage/webappapis.html#events)
You use the event name with addEventListener, and the event handler name for the event handler attribute.

You'll note that even W3Schools talks about the "ended event" too.
 
So, here is a little JSNI function to do it:

public static native final void listenForEnd(AudioElement aud, Command onComplete) /*-{
   aud.onended = function() {
      onComplete.@com.google.gwt.user.client.Command::execute(*)();
   };
}-*/
;

Hope that helps someone.

In which browser is this not working?

Because addEndedHandler works for me in Chrome and Firefox with the following code:

Audio audio = Audio.createIfSupported();
audio.setControls(true);
audio.setAutoplay(true);
audio.addEndedHandler(e -> Window.alert("Ended"));
RootPanel.get().add(audio);
 

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment