HTML5 audio download event?

Is there a download event that fires when audio download finishes? I am creating an audio element like this.

var myAudio = new Audio("mySound.mp3"); myAudio.load(); 

Tried to add eventListener like that, but it doesn't seem to work.

 myAudio.addEventListener("load",soundLoaded,false); 
+6
source share
2 answers

It looks like you need a canplaythrough event. This works when the browser believes that it can play the entire audio file without stopping.

Try:

 myAudio.addEventListener('canplaythrough', soundLoaded, false); 

There are 7 events that fire in this order when the audio file is uploaded:

  • loadstart
  • durationchange
  • loadedmetadata li>
  • loadeddata li>
  • progress
  • canplay
  • canplaythrough

Please note that this is not supported in versions of Internet Explorer prior to 9.

+12
source

The implementation of an audio tag is highly browser dependent. Its support, I would say, is less than a video tag, as it was in the hype after Steve Jobs talk about Flash against the holy war of HTML5, it is funny enough that it is least supported by Safari. And it’s true that it works well for the Video tag (on the event handler), does not work for the Audio tag, but it’s good that the statuses are still true. For instance:

 var a = new Audio(); 
  • a.networkState and a.readyState - checking this every second on the timer, you can easily get an idea of ​​the loading and progress of the playback.

Other interesting properties:

  • seeking - True if UA is currently searching
  • seekable is a TimeRange object that can be accessed.
  • played - TimeRange that UA was played.
  • paused - True if playback is paused.
  • ended - true if playback is finished.
  • currentTime - Returns / sets the playback position in seconds.
  • duration

Remember to use canPlayType(type) - returns "probably" and "maybe"

Update: consider viewing SoundManager2 - http://www.schillmania.com/projects/soundmanager2/

+3
source

Source: https://habr.com/ru/post/1397135/


All Articles