Skip to content

Instantly share code, notes, and snippets.

@lassiter
Forked from R-V-S/player-js-analysis.md
Last active November 29, 2017 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lassiter/8e690d16357ebdb8e849f342cebb7d05 to your computer and use it in GitHub Desktop.
Save lassiter/8e690d16357ebdb8e849f342cebb7d05 to your computer and use it in GitHub Desktop.
player.js analysis
  1. This file declares a class, Player, instantiates it, and assigns it to a global player variable.

  2. The Player class contains four methods:

    • constructor()
    • playPause()
    • skipTo()
    • setVolume()
  3. The constructor() method sets initial values for the currentlyPlaying, playState, volume, and soundObject properties.

    • currentlyPlaying is set to the first item in album.songs.
    • The initial playState is "stopped".
    • The volume is set to the number 80.
    • The soundObject instantiates a new buzz.sound object using the soundFileUrl property of this.currentlyPlaying. The buzz variable doesn't appear to be initialized here, so presumably it's a dependency loaded elsewhere.
  4. The playPause() method accepts one parameter, song. It sets it to this.currentlyPlaying by default.

    It checks to see if this.currentlyPlaying is different from song, and if so, it:

    • Stops the soundObject property.
    • Removes the "playing" and "paused" classes from the element property of this.currentlyPlaying.
    • Sets this.currentlyPlaying to the function's parameter, song.
    • Changes the playState property to "stopped".
    • Instantiates a new sound object using this.currentlyPlaying, which was just updated to song. It checks to see if this.playState is paused or stopped, and if so, it:
    • Sets the volume to the default.
    • Toggles play on the object.
    • Changes playState to playing and replaces the class paused with playing. If neither of these if funtions trigger then an else function runs:
    • The soundObject and playState is set/toggled to pause.
    • playing is replaced by paused as a class for currentlyPlaying.
  5. The skipTo method accepts one parameter, percent. If the playState does not equal playing it should return. Otherwise, it uses the method setTime to take the input parameter of skipTo and divides it by 100 then multiplies it by the getDuration output.

  6. The setVolume method accepts one parameter, percent.

    • It sets volume to the input of percent.
    • It passes itself (setVolume) as a method to adjust the soundObject to the percent input.
  7. getDuration returns the method getDuration of the soundObject.

  8. getTime returns the method getTime of soundObject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment