Callbacks

There are a variety of callbacks specific to AmplitudeJS that get called at certain times that the developer can bind to.

CallbackCalled When
stopThe active audio is stopped
initializedAmplitudeJS has finished initializing
song_repeatedWhen the active audio has been repeated
nextWhen the next audio has been played
prevWhen the prev audio has been played
album_changeWhen the album has changed
song_changeWhen the song has changed
playlist_changedWhen the playlist has changed.

To bind to a callback you add a function to your callbacks object with the key of one of the callbacks listed above. That key will be a function. When the callback is called, the function the user passes will be run. For example, after the user clicks play we want to increase the play count. I'd set up a callback that has a method to increase the play count:

Example of a callback when AmplitudeJS is stopped

Amplitude.init({
    songs: [
        {
                "name": "Song Name 1",
                "artist": "Artist Name",
                "album": "Album Name",
                "url": "/song/url.mp3",
                "cover_art_url": "/cover/art/url.jpg"
        },
        {
                "name": "Song Name 2",
                "artist": "Artist Name",
                "album": "Album Name",
                "url": "/song/url.mp3",
                "cover_art_url": "/cover/art/url.jpg"
        },
        {
                "name": "Song Name 3",
                "artist": "Artist Name",
                "album": "Album Name",
                "url": "/song/url.mp3",
                "cover_art_url": "/cover/art/url.jpg"
        }
    ],
    callbacks: {
        stop: function(){
            console.log("Audio has been stopped.")
        }
    }
});

Every time the audio has been stopped, a message is printed to the console.

There are also the native HTML 5 Audio events that the developer can bind too. For more descriptions on when these events are propagated check out: https://www.w3schools.com/tags/ref_av_dom.asp

The list of native HTML 5 Audio Events that AmplitudeJS listens to are:

  • abort
  • error
  • loadeddata
  • loadedmetadata
  • loadstart
  • pause
  • playing
  • play
  • progress
  • ratechange
  • seeked
  • seeking
  • stalled
  • suspend
  • timeupdate
  • volumechange
  • waiting
  • canplay
  • canplaythrough
  • durationchange
  • ended

Specifically the timeupdate callback is super helpful because this gets triggered when the song time updates. This can be used to call other AmplitudeJS events such as song played percentage and set 3rd party visualizations.