- 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 ifthis.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
-
-
Save stacietaylorcima/455f6f1a249fdc4530735c7c45c2abd1 to your computer and use it in GitHub Desktop.
player.js analysis
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Analysis of player.js
PlayerclassThis file declares a class,
Player, instantiates it, and assigns it to a globalplayervariable.The
Playerclass contains six methods:-
constructor()-
getDuration()-
getTime()-
playPause()-
skipTo()-
setVolume()constructor()MethodThe
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.getDuration()MethodBased solely on the name of this method and the property referenced, I presume it allows the player to display the length of the song.
The
getDuration()method accesses the objectsoundObjectand returns the value of the propertygetDuration.getTime()MethodBased solely on the name of this method and the property referenced, I presume it allows the player to display the place in the length of the song that it is at in its duration.
The
getTime()method accesses the objectsoundObjectand returns the value of the propertygetTime.playPause()MethodBased solely on the name of this method, I presume (at a high-level) it allows the player to start and stop the playing of songs.
The
playPause()method accepts one argument,song. It sets it tothis.currentlyPlayingby default. Then executes the following code:If
this.currentlyPlayingis different fromsong, execute the following code:soundObjecteven if nothing is playing."playing"and"paused"classes from theelementproperty ofthis.currentlyPlaying.this.currentlyPlayingto the function's parameter,song.playStateproperty to"stopped".soundObjectinstantiates a newbuzz.soundobject using thesoundFileUrlproperty ofthis.currentlyPlaying.If the
playStateis'paused'or'stopped'setVolumeplayState to'playing'`"paused"classes from theelementproperty ofthis.currentlyPlayingand adds the"playing"class.If the
playStateis anything else besides the aforementioned"paused"or"stopped", execute the following code:pausethe'soundObject'playStateto'paused''playing'class and add'paused'classskipTo()MethodBased solely on the name of this method, I presume it allows the user to select any point in the song to start playing from.
The
skipTo()method takes an argumentpercentand executes the following code:playState is not"playing"` return that and execute the following code:percentargument that was passed through and something withgetDuration. I don't fully understand what that does yet.setVolume()MethodBased solely on the name of this method and the property referenced, I presume it allows the player to display the current volume level and the user to select a new volume level.
The
setVolume()method takes one argumentpercentand sets the volume- GET the value of the volume
- In the
soundObjectobject, SET the value of the propertysetVolumeto thepercentargument that was passed throughplayerVariableThe player variable instantiates a new Player something. I don't fully understand what that does yet.