-
This file declares a class,
Player, instantiates it, and assigns it to a globalplayervariable. -
The
Playerclass contains four methods:constructor()playPause()skipTo()setVolume()
-
The
constructor()method sets initial values for thecurrentlyPlaying,playState,volume, andsoundObjectproperties.currentlyPlayingis set to the first item inalbum.songs.- The initial
playStateis"stopped". - The
volumeis set to the number80. - The
soundObjectinstantiates a newbuzz.soundobject using thesoundFileUrlproperty ofthis.currentlyPlaying. Thebuzzvariable doesn't appear to be initialized here, so presumably it's a dependency loaded elsewhere.
-
The
playPause()method accepts one parameter,song. It sets it tothis.currentlyPlayingby default.- It checks to see if
this.currentlyPlayingis different fromsong, and if so, it:- Stops the
soundObjectproperty. - Removes the
"playing"and"paused"classes from theelementproperty ofthis.currentlyPlaying. - Sets
this.currentlyPlayingto the function's parameter,song. - Changes the
playStateproperty to"stopped". - Instantiates a new sound object using
this.currentlyPlaying, which was just updated tosong.
- Stops the
- It checks to see if
this.playStateis equal topausedorstopped, if so, it starts playing by doing the following:- Calls the
setVolumemethod ofthis.soundObjectand passesthis.volumeas a parameter. - Calls the
playmethod ofthis.soundObject. - Sets
this.playStateto"playing". - Removes the class
"paused"fromthis.currentlyPlaying.elementand adds the class"playing".
- Calls the
- Else it pauses playback by doing the following:
- Calls the
pausemethod inthis.soundObject. - Sets
this.playStateto"paused". - Removes the
"playing"class fromthis.currentlyplaying.elementand adds the"paused"class.
- Calls the
- It checks to see if
-
The
skipTo()method accepts one parameter,percent. It skips the song to the percentage value ofpercent.- It checks if
this.playState !== "playing"and exitsskipToiftrue. - The
setTimemethod ofthis.soundObjectis called with a parameter:(percent/100) * this.soundObject.getDuration().
- It checks if
-
The
setVolume()method accepts one parameter,percent. It sets the volume of the player to the passed percentage:- It sets
this.volumetopercent. - It calls the
setVolumemethod ofthis.soundObjectand passespercentas a parameter.
- It sets
-
-
Save DamianRivas/18e1933706124b22767e3e15c4703602 to your computer and use it in GitHub Desktop.
player.js analysis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment