Class Player

A Player instance plays collections of audio files. The player can be instructed to play audio files from a station (Station) or a simple array of AudioFiles. Once provided with a station or array of audio files (via playStation or playAudioFiles), the player maintains an index into the collection and plays the files, one by one, until the end of the collection is reached. The player can pause and resume playback, skip past an audio file that is currently playing, restart playback of an audio file, and go back to a file that appears earlier in the provided collection.

A player instance requires token and secret values that are used to authenticate with the Adaptr servers and identify which playlists/stations should be made available to the player.

initialize must be called before the player is useful in any way.

Stations are provided by the player upon initialization. A 'playlist', published via the Adaptr.com web dashboard, equates to a 'station' in the SDK.

Individual audio files for playback can be discovered by listing the contents of stations (fetchAudioFilesInStation) or by searching through all available audio files (searchAudioFiles).

The Player emits various events that can be subscribed to with on and once. Event handlers are passed an argument with details of the event (see the definition of Event here), and an optional data item related to the event (event.data). The following events are broadcast in the course of the player's existance:

event event.data type description
playing the player has started (or is trying to) play music
paused the player has paused playback of music
idle the player has stopped playback of music
music-unavailable user may not play music, likey due to licensing constraints
music-exhausted the player has reached the end of a station or play queue and has stopped playback
play-started Play a song has begun playback. The play object is provided as an argument to the event handler
play-resumed Play a song has resumed playback after being paused. The play object is provided as an argument to the event handler
play-paused Play the current song has paused playback. The play object is provided as an argument to the event handler
play-ended Play the current song has ended playback. The play object is provided as an argument to the event handler
volume 0-100 the player volume has been adjusted. The value (from 0-100) is provided as an argument to the event handler
stations Station[] the player has initialized and received a list of music stations from the server. The array of stations are passed to this handler.
station-changed Station the player has switched to play a station. The station is passed to the handler.
invalid-credentials the token/secret value passed to the player didn't pass muster with the adaptr.com servers

Hierarchy

  • EventEmitter
    • Player

Constructors

  • Create a new Adaptr Player instance. This creates the player object, but it will not be useful until initialize is called to request available music from the Adaptr servers.

    Credentials are validated in the initialize call, so no error will be thrown here if they are not valid.

    Parameters

    • token: string

      The token value associated with your Adaptr app, provided via adaptr.com/dashboard

    • secret: string

      The secret value associated with your Adaptr app, provided via adatpr.com/dashboard

    • opts: PlayerOptions = {}

    Returns Player

Methods - Client Playlist

  • Remove a client-generated playlist from the adaptr servers.

    Parameters

    • id: string

      id of a previously created client-generated playlist

    Returns Promise<void>

  • Persist a client-generated playlist. This method allows SDK users to persist collections of AudioFiles on the Adaptr servers for later retrieval.

    note - this 'playlist' is distinct from the 'playlists' generated in the adaptr.com dashboard. These playlists are meant to be created by end-users of the application and are not (currently) sharable with other users.

    Parameters

    • clientPlaylist: ClientPlaylist

      client-side playlist to persist on adaptr servers

    Returns Promise<void>

Methods - Discovery

  • Retrieve a PageOfAudioFiles in the station with the given name. Note that the page offset is 0-based.

    Parameters

    • stationOrStationName: string | Station
    • page: number = 0

      0-base page offset

    • perPage: number = 20

      number of AudioFile to return per page (up to 100).

    Returns Promise<PageOfAudioFiles>

  • Retrieve a station with the given name, or null

    Parameters

    • name: string

      name of a station available from getStations

    Returns Station

  • Return available stations. This method will throw an exception if initialize has not been called or resolved successfully.

    If you're willing to wait, you can use the promise returned from whenAvailable or initialize instead of polling this property.

    Returns Station[]

    stations available to the player for playback

  • Search through all available stations for songs similar to the provided query. This method sorts the results based on how similar they are to the query. Note that a non-empty query must be provided. Searches can be limited to a specific, station, and the number of results can be adjusted via the options.

    Parameters

    • q: string

      simple text query that will run again song title, artist, album text

    • opts: SearchOptions = {}

      search options

    Returns Promise<PageOfSearchResults>

Methods - Event

  • Deregister an event handler. Deregister all event handlers by not passing any arguments. Deregister all handlers for specific events by only passing the first argument.

    Parameters

    • Optional type: string
    • Optional handler: EventHandler

    Returns void

  • Register a callback to receive events triggered by the player.

    Parameters

    • type: string

      name of event, or a space-separated list of events

    • handler: EventHandler

      event handler

    Returns boolean

  • Register a callback to receive at most a single call for the named event(s).

    Parameters

    • type: string

      name of event, or a space-separated list of events

    • handler: EventHandler

      event handler

    Returns boolean

Methods - Feedback

  • Notify that the user did not like this song play.

    Parameters

    • play: Play

      a Play object, containing an AudioFile, that the user does not like

    Returns Promise<void>

  • Mark the given song as a favorite, and store on adaptr servers.

    Parameters

    • audioFile: AudioFile

      audio file to mark as a favorite

    Returns Promise<void>

  • Retrieve a page of audio files from the server that the user has favorited. The audio files are sorted by date they were favorited, with the most recently favorited files appearing first.

    Parameters

    • page: number = 0

      0-based page index to retrieve

    • perPage: number = 20

      number of audio files to return per page

    Returns Promise<PageOfAudioFiles>

  • Notify that the user 'likes' this song play.

    Parameters

    • play: Play

      a Play object, containing an AudioFile, that the user likes

    Returns Promise<void>

  • Remove the 'favorite' status for the given audio file, and tell the adaptr servers.

    Parameters

    Returns Promise<void>

  • Remove a previous 'like' or 'dislike' indicator for the given song play.

    Parameters

    • play: Play

      Remove a like or dislike indicator for a Play

    Returns Promise<void>

Methods - Initialize

  • Initialize the player instance by validating the credentials and retrieving a list of available Stations from the Adaptr servers. This method returns a Promise that will resolve to a Session with a list of Station objects or reject if the credentials are not valid or no music is available for the current user. The promise returned by this method must be resolved before the player can perform any playback.

    Subsequent calls to this method will stop music playback and reinitialize the instance. If you just want access to the Session object, then use whenAvailable.

    Returns Promise<Session>

  • Modern browsers require audio to be initialized and started in response to a user-interaction event (such as a 'click' event handler). Calls to playStation or playAudioFiles will try to initialize audio, but if the first call to either method wasn't initiated in an event handler, audio initialization will fail. This method may be called while in an event handler to handle audio initialization before those play methods are called. The method doesn't return any values, and may be called repeatedly without affecting anything if audio is already initialized.

    Returns void

  • If the player has been initialized, then this returns the promise from that original call. If the player has not been initialized, then this method calls initialize and returns that value.

    Returns Promise<Session>

Methods - Play Queueing

  • Stop any playback currently in progress and immediately start playing the given array of audio files. Playback will stop when all files have been played.

    This call must either be made from a user-initiated event handler (like a 'click' event handler) or a call to initializeAudio must have previously been made to initialize audio playback.

    Parameters

    • audioFiles: AudioFile[]

      audio files to play or append to the current playback queue

    • options: QueueOptions = {}

      playback options

    Returns Promise<void>

  • Stop any playback currently in progress and immediately start playing a preview clip of the given audio file.

    This call must either be made from a user-initiated event handler (like a 'click' event handler) or a call to initializeAudio must have previously been made to initialize audio playback.

    Parameters

    • audioFile: AudioFile

      the audio file to play a preview of

    Returns Promise<void>

  • Immediately begin playing songs in a Station with the given name.

    This will stop any existing playback.

    Available stations are returned via initialize or getStations or the 'stations' event.

    Parameters

    Returns void

  • Stop any playback currently in progress and queue up an array of audio files for playback. If playImmediately is true, then kick off playback of the queue, otherwise, the player will remain idle.

    Parameters

    • audioFiles: AudioFile[]

      audio files to play or append to the current playback queue

    • options: QueueOptions = {}

      playback options

    Returns Promise<void>

  • Stop any playback currently in progress and queue up an a preview of the given audio file for playback. If playImmediately is true, then kick off playback of the sample, otherwise, the player will remain idle.

    Parameters

    Returns Promise<void>

  • Tune to the given station and load the first song into memory for immediate playback with play.

    This will stop any existing playback, unless the same station is currently being played.

    Parameters

    Returns void

Methods - Playback

  • Stop playback of the current song, move to the previous song in the current station or queue, and begin playback of that song.

    Returns void

  • Retrieve metadata about the song currently playing or paused, or null if no song is playing.

    Returns Play

  • Retrieve metadata about the currently playing or prepared station, or null if no station is active or if the player is just playing a list of AudioFiles.

    Returns Station

  • Retrieve the player's current PlayerState.

    Returns PlayerState

  • Return current volume

    Returns number

    0-100

  • True if the player is currently muted.

    Returns boolean

  • Pause audio playback

    Returns void

  • Resume playback of the current station or queued audio files, or start playback after a call to prepareAudioFiles or prepareStation.

    Returns void

  • Restart playback of the current song

    Returns void

  • Mute or unmute the player. This differs from just using setVolume in that the volume before muting is saved, so unmuting returns to that original volume.

    Parameters

    • isMuted: boolean

      true to mute, false to unmute

    Returns void

  • Set the current player volume (0-100)

    Parameters

    • vol: number

      0-100

    Returns void

  • Skip past the current song and advance to the next one. In the future, skips may be disallowed for certain stations/playlists, and this method will resolve to false if the skip was disallowed.

    Returns Promise<boolean>

  • Stop audio playback and empty out the play queue. Future calls to play will have no effect until playAudioFiles or playStation is called.

    Returns void

Generated using TypeDoc