By: Paul S. Cilwa Viewed: 4/6/2020
Posted: 11/17/2017
Topics/Keywords: #Computers #Programming #Projects #WebAudioAPI #JavaScript #MusicPlayer #Cross-fadingMusicPlayer #OrganicaAudio Page Views: 929
Representing a single music track for the Web Audio API.

It's now time to open the empty OrganicaAudio.js file and create the constructor for the object that will encapsulate the Web Audio API. We can start by typing in some preliminary lines:

/* OrganicaAudioTrack

function OrganicaAudioTrack(aSource)
	this.Context = MyOrganicaAudio.Context; // For convenience
	this.Filename = aSource;

	this.StartCrossFade = 0;
	this.Loaded = false;
	this.Loading = false;
	this.Playing = false;

Note the use of the this keyword. It represents the object being created; statements such as

this.Loaded = false;

simultaneously create an object property and assign it a value.

In JavaScript, one cannot always count on this, because in the morass of callback functions and asynchronous operations this isn't always what you think it will be. It's always safe to use this in a constructor, however, along with any function you can guarantee won't be used as a callback function.

The property names I made up—"Filename", "Loading"—are guesses as to what I'll need to implement my player. I may add more properties as I discover I need them, or remove any I find unneccessary.

Obviously this was a small step. The next one will require more of us, however; as it requires us to learn another new JavaScript feature: Promises.