Nation JavaScript Library v2

BasicAudioPlayer

function
BasicAudioPlayer()

Option name Type Description
selector domelement_or_querystring_or_jqueryobject

The element that contains a .js-player or .player element

options object

An object containing the settings for this audio player
controls {boolean=true} If true, the browser's default audio controls will be shown
autoPlay {boolean=false} If true, the audio will be begin playing as soon as it has started to load. Note that this will not work on mobile devices
loop {boolean=false} Loops the audio infinitely if true
preload {boolean=false} Start loading the audio file straight away. This doesn't make any difference on mobile devices, where bandwidth is protected. In those cases, the file will not start to download until the user presses the play button

Dependencies:

NATION.Utils
NATION.EventDispatcher

About:

HTML5 audio player. Can be used with NATION.MediaControls for custom controls

Supports MP3, OGG, and WebM audio file formats, each of which are optional

To immediately start an audio file loading, include one of the following data attributes on the element represented by 'selector': data-mpeg, data-ogg, data-webm

Example

PLAYER_READY

property
BasicAudioPlayer.prototype.PLAYER_READY

Event that fires when the current track is ready to play

AUDIO_PLAYING

property
BasicAudioPlayer.prototype.AUDIO_PLAYING

Event that fires when the audio starts playing

AUDIO_PAUSED

property
BasicAudioPlayer.prototype.AUDIO_PAUSED

Event that fires when the audio is paused

AUDIO_COMPLETE

property
BasicAudioPlayer.prototype.AUDIO_COMPLETE

Event that fires when the audio completes

LOAD_PROGRESS

property
BasicAudioPlayer.prototype.LOAD_PROGRESS

Event that fires when some of the audio file is loaded

PLAY_PROGRESS

property
BasicAudioPlayer.prototype.PLAY_PROGRESS

Event that fires regularly while audio file is playing

META_LOADED

property
BasicAudioPlayer.prototype.META_LOADED

Event that fires when the audio file's meta data has been loaded

getHasPlayTriggered

method
BasicAudioPlayer.prototype.getHasPlayTriggered() ->boolean

Check if the current audio file has ever been played

Useful for mobile devices where you might want an external play button (which wont work until the audio file has been played using native controls)

getCurrentTime

method
BasicAudioPlayer.prototype.getCurrentTime() ->boolean

Returns the current play position (in seconds)

getPlayProgress

method
BasicAudioPlayer.prototype.getPlayProgress() ->number

Returns the current progress percentage (0-1)

getLoadProgress

method
BasicAudioPlayer.prototype.getLoadProgress() ->number

Returns percentage of current audio loaded

getDuration

method
BasicAudioPlayer.prototype.getDuration() ->number

Returns duration of current track (in seconds)

getDurationText

method
BasicAudioPlayer.prototype.getDurationText() ->string

Returns string with duration in 0:00:00 format

getCurrentTimeText

method
BasicAudioPlayer.prototype.getCurrentTimeText() ->string

Returns string with current time in 0:00:00 format

isReady

method
BasicAudioPlayer.prototype.isReady() ->boolean

Returns whether player can be used yet

isPlaying

method
BasicAudioPlayer.prototype.isPlaying() ->boolean

Returns if video is currently playing

play

method
BasicAudioPlayer.prototype.play()

Plays the current track

pause

method
BasicAudioPlayer.prototype.pause()

Pauses the current track, if it's playing

mute

method
BasicAudioPlayer.prototype.mute()

Mutes the current track

unMute

method
BasicAudioPlayer.prototype.unMute()

Unmutes the current track

seekTo

method
BasicAudioPlayer.prototype.seekTo()

Option name Type Description
time number

Time, in seconds, to seek to

Seek audio to position

playNewTrack

method
BasicAudioPlayer.prototype.playNewTrack()

Option name Type Description
autoPlay boolean

Start playing the new track immediately

options object

Object contianing the settings for the new track
mp3URL {string=""} The URL to the mp3 file
oggURL {string=""} The URL to the ogg file
webmURL {string=""} The URL to the webm audio file

Play a new track (stops any currently playing)

rebuild

method
BasicAudioPlayer.prototype.rebuild()

Rebuild the player completely
This calls remove() and then creates a brand new HTML5 audio player using the last set audio URLs

remove

method
BasicAudioPlayer.prototype.remove()

Stops audio and removes player from the DOM