Skip to content

BuildFire Audio Player Service

o5faruk edited this page May 2, 2021 · 13 revisions

⚠️⚠️⚠️

This sdk documentation is deprecated and will not be updated. Check out our new docs at https://sdk.buildfire.com/docs/audio-player/

⚠️⚠️⚠️

buildfire.services.media.audioPlayer

This is a service that lives on the app user level. It is meant to allow plugins to play audio on the app without interruption from user navigation. It facilitates common audio controls listed below. Also maintains a playlist for the user to access independent of a single plugin.

audioPlayer

Typically used on the Widget side to but can also be used on the Control if needed.

Requirements

From the Widget <script src="../../../scripts/buildfire/services/media/audioPlayer.js"></script>

From the Control <script src="../../../../scripts/buildfire/services/media/audioPlayer.js"></script>

Example

https://github.com/BuildFire/simpleBuildFireJSExamples/tree/master/exampleAudioPlayer

Constructor

none. The audio player is a singleton

Methods

  • play(track): this method is used to begin playing a track
    • track is an optional argument
      • if null' or undefined` it will try to resume the paused track if any
      • if string then it assumes its the url of an audio file you want to play
      • if number then it tries to find that track index from the playlist
      • if object then it assumes its of type Track see below
  • pause() : this will pause the current track
  • isPaused(callback) : this will check if audio is playing or not.
    • callback is a function(err, paused) where paused is a boolean
  • skip(sec): skips forward the indicated seconds forward
  • next(): plays the next item in the playlist
  • previous(): plays the previous item in the playlist
  • loopTrack(): keeps playing the current track in a loop
  • getPlaylist(callback) : calls the callback function with the user playlist
  • addToPlaylist(track): adds track to playlist
  • removeFromPlaylist(index): removes track index from playlist
  • getCurrentTrack(callback): calls the callback function with the value of the current track
  • setTime(sec): sets the position of the current time of the current audio track
  • setVolume(percent): sets the volume for audio playback , the value should be between 1 and 100 .

Properties

  • settings: is a property that consists of the following methods:
    • get(callback): calls the callback function with the current settings
    • set(settings): sets the user audio player settings with the passed argument

Handlers

  • onEvent(callback): calls the callback function whenever an event occurs
    • callback(e): e is an object with the follow properties
      • data: this value will be determined by the event type
      • event : a string with the following possible values
        • play : triggered when a track plays. data will consist of
          • data.track: current track
          • data.index: index of track
        • pause : triggered when a track is paused.
        • resume : triggered when a track is un-paused.
        • next : triggered when the next track plays. data will consist of
          • data.track: next track
        • previous : triggered when the previous track plays. data will consist of
          • data.track: previous track
        • removeFromPlaylist : triggered when a track is removed from the playlist. data will consist of
          • data.newPlaylist: new playlist
        • audioEnded : triggered when a track ends.
        • timeUpdate : triggered when a tracks time changes. data will consist of
          • data.currentTime: current position in the track in seconds
          • data.duration: length of track in seconds
          • data.buffer: object with start and end of buffer times if any

Helper Classes

Track

function Track(title, url, image, album,artist) {
    this.title = title;
    this.url = url;
    this.image = image;
    this.album = album;
    this.artist=artist;
    this.startAt = 0; // where to begin playing
    this.lastPosition = 0; // last played to
}

AudioSettings

function AudioSettings(){
    this.autoPlayNext = false; // once a track is finished playing go to the next track in the play list and play it
    this.loop = false; // once the end of the playlist has been reached start over again
    this.autoJumpToLastPosition= false; //If a track has [lastPosition] use it to start playing the audio from there
    this.shufflePlaylist =false;// shuffle the playlist
}

Clone this wiki locally