Home Manual Reference Source
import AudioPlayer from 'itslanguage/audio/audio-player.js'
public class | source

AudioPlayer

ITSLanguage AudioPlayer non-graphical component.

Constructor Summary

Public Constructor
public

constructor(options: Object)

Construct an AudioPlayer for playing .wav or .mp3 files.

Member Summary

Public Members
public
Private Members
private
private
private
private
private
private

Method Summary

Public Methods
public

addEventListener(name: string, handler: Function)

Add an event listener.

public

Bind a stopwatch to sync with the playing and stopping functionality of the player.

public

Returns ready state of the player.

public

Gets the audio level of the current loaded audio.

public

Returns the percentage of which the buffer is filled.

public

Returns the current playing time as offset in seconds from the start.

public

Returns the total duration in seconds.

public

Get the playback rate of the current loaded audio.

public

Return the muted state of the current loaded audio.

public

Check if there is playback in progress.

public

load(url: string, preload: boolean, loadedCb: Function)

Preload audio from an URL.

public

pause()

Pause playback of audio.

public

play(position: number)

Start or continue playback of audio.

public

Start preloading audio.

public

Remove an event listener of the player.

public

reset()

Unload previously loaded audio.

public

Turn off all event listeners for this player.

public

scrub(percentage: number)

Start playing audio at the given offset.

public

setAudioMute(shouldMute: boolean)

Manually set the muted state of the current loaded audio.

public

Sets the audio level of the current loaded audio.

public

Set the playback rate of the audio.

public

stop()

Stop playback of audio.

public

Toggle the current playing audio to be muted or not.

public

Toggle audio playback.

Private Methods
private

_getPlayer(callbacks: Function): *

Get a player object that performs audio compression, when available.

private

Check for mandatory browser compatibility.

Public Constructors

public constructor(options: Object) source

Construct an AudioPlayer for playing .wav or .mp3 files.

Params:

NameTypeAttributeDescription
options Object
  • nullable: true

Override any of the default settings.

Emit:

Event

'playbackstopped' When playback has ended, been stopped or been paused.

Event

All events the HTML5 Audio also fires. {@link http://www.w3schools.com/tags/ref_av_dom.asp}

Public Members

public canUseAudio: * source

Private Members

private _audioLevel: number source

private _audioMuted: boolean source

private _emitter: * source

private _player: WebAudioPlayer source

private _settings: * source

private _stopwatch: * source

Public Methods

public addEventListener(name: string, handler: Function) source

Add an event listener. Listens to events emitted from the player.

Params:

NameTypeAttributeDescription
name string

Name of the event.

handler Function

Handler function to add.

public bindStopwatch(tickCb: Function): Stopwatch source

Bind a stopwatch to sync with the playing and stopping functionality of the player.

Params:

NameTypeAttributeDescription
tickCb Function

Callback to invoke on every tick. A tick occurs once every 100 ms.

Return:

Stopwatch

New Stopwatch object.

Throw:

Error

If _tickCb is null.

public canPlay(): boolean source

Returns ready state of the player.

Return:

boolean

True when player is ready to start loading data or play. False when no audio is loaded or the player is preparing.

public getAudioVolume(): number source

Gets the audio level of the current loaded audio. Valid values are from 0 (0%) to 1 (100%).

Return:

number

Volume level of the current loaded audio.

public getBufferFill(): number source

Returns the percentage of which the buffer is filled.

Return:

number

Percentage of buffer fill.

public getCurrentTime(): number source

Returns the current playing time as offset in seconds from the start.

Return:

number

Time in seconds as offset from the start.

public getDuration(): number source

Returns the total duration in seconds.

Return:

number

Time in seconds of fragment duration.

public getPlaybackRate(): number source

Get the playback rate of the current loaded audio.

Return:

number

Playback rate of the audio.

public isAudioMuted(): boolean source

Return the muted state of the current loaded audio.

Return:

boolean

The muted state of the current loaded audio.

public isPlaying(): boolean source

Check if there is playback in progress.

Return:

boolean

True if user is currently playing audio. False otherwise.

public load(url: string, preload: boolean, loadedCb: Function) source

Preload audio from an URL.

Params:

NameTypeAttributeDescription
url string

The URL that contains the audio.

preload boolean
  • optional
  • default: true

Try preloading metadata and possible some audio. Set to false to not download anything until playing.

loadedCb Function
  • nullable: true

The callback that is invoked when the duration of the audio file is first known.

Emit:

Event

'canplay' When the player is ready to play.

public pause() source

Pause playback of audio. Stops the stopwatch.

public play(position: number) source

Start or continue playback of audio. Also starts the stopwatch at the given position.

Params:

NameTypeAttributeDescription
position number
  • nullable: true

When position is given, start playing from this position (seconds).

public preload() source

Start preloading audio.

public removeEventListener(name: string, handler: Function) source

Remove an event listener of the player.

Params:

NameTypeAttributeDescription
name string

Name of the event.

handler Function

Handler function to remove.

public reset() source

Unload previously loaded audio. Stops the player and any stopwatch.

Emit:

Event

'unloaded'

public resetEventListeners() source

Turn off all event listeners for this player.

public scrub(percentage: number) source

Start playing audio at the given offset. Corrects a percentage under 0 or above 100 to the respective values.

Params:

NameTypeAttributeDescription
percentage number

Start at this percentage (0..100) of the audio stream.

public setAudioMute(shouldMute: boolean) source

Manually set the muted state of the current loaded audio.

Params:

NameTypeAttributeDescription
shouldMute boolean

Whether the audio should be muted or unmuted.

public setAudioVolume(volume: number) source

Sets the audio level of the current loaded audio. Valid values are from 0 (0%) to 1 (100%).

Params:

NameTypeAttributeDescription
volume number

Volume value from 0 to 1.

public setPlaybackRate(rate: number) source

Set the playback rate of the audio. Values are used according to HTML5 Audio. Example values: 1.0 is normal speed. 0.5 is half speed (slower). 2.0 is double speed (faster). -1.0 is backwards, normal speed. *-0.5 is backwards, half speed.

Params:

NameTypeAttributeDescription
rate number

Rate at which to change the audio playback.

public stop() source

Stop playback of audio. Stops and resets the stopwatch.

public toggleAudioMute() source

Toggle the current playing audio to be muted or not. If the audio will be muted, the current audio level is remembered and can be unmuted to continue at this same audio level.

public togglePlayback() source

Toggle audio playback. Switch from playing to paused state and back.

Private Methods

private _getPlayer(callbacks: Function): * source

Get a player object that performs audio compression, when available.

Using the Media Stream Recording API for recording is the preferred solution. It allows recording compressed audio which makes it quicker to submit. If not available, use a default createScriptProcessor is used.

Params:

NameTypeAttributeDescription
callbacks Function

Callbacks to add to the chosen player.

Return:

*

private _playbackCompatibility() source

Check for mandatory browser compatibility. Logs detailed browser compatibilities related to for audio playback.

Throw:

Error

If no native wave or MP3 playback is available.