Update Modello web samples from upstream; version up
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / MediaPlayer / project / js / audioPlayer.js
index 3081f78..5a5dafd 100644 (file)
@@ -7,12 +7,19 @@
  *
  */
 
+var mainArtistText;
+var songDurationSec;
+
 AudioPlayer = function()
 {
        this.clearAudioTimeInterval = undefined;
        this.currentAudioContent = undefined;
 }
 
+/*
+ * Play function for when play button is clicked
+ */
+
 AudioPlayer.prototype.play = function()
 {
        console.log("MediaPlayer in AudioPlayer::play");
@@ -30,6 +37,7 @@ AudioPlayer.prototype.play = function()
                                var timeoutFunction = function()
                                {
                                        localStorage.prevAudioTime = this.playerControls.currentTime;
+                                       this.updateTrackTime();
                                }
 
                                this.clearAudioTimeInterval = setInterval(timeoutFunction.bind(this),500);
@@ -40,6 +48,10 @@ AudioPlayer.prototype.play = function()
                console.log(this.content[this.listIndex].artists[0] + " : " + this.content[this.listIndex].title + " can't play yet, it hasn't loaded");
 }
 
+/*
+ * Pause function for when pause button is clicked
+ */
+
 AudioPlayer.prototype.pause = function()
 {
        if (!this.playerControls.paused)
@@ -49,10 +61,9 @@ AudioPlayer.prototype.pause = function()
        }
 }
 
-AudioPlayer.prototype.playing = function()
-{
-       return !(this.playerControls.paused);
-}
+/*
+ * Next function for when next button is clicked
+ */
 
 AudioPlayer.prototype.next = function()
 {
@@ -67,6 +78,10 @@ AudioPlayer.prototype.next = function()
        }
 }
 
+/*
+ * Previous function for when previous button is clicked
+ */
+
 AudioPlayer.prototype.previous = function()
 {
        if (this.content)
@@ -80,6 +95,19 @@ AudioPlayer.prototype.previous = function()
        }
 }
 
+/*
+ * Returns play state
+ */
+
+AudioPlayer.prototype.playing = function()
+{
+       return !(this.playerControls.paused);
+}
+
+/*
+ * Load the file located at the index.  If play is true, play immediately once file is loaded
+ */
+
 AudioPlayer.prototype.load = function(index, play)
 {
        this.listIndex = index;
@@ -90,17 +118,18 @@ AudioPlayer.prototype.load = function(index, play)
        this.playerControls.load();
 }
 
+/*
+ * Redraws the media bar with the new artist, title, and cover album
+ */
+
 AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
 {
-       var playBarHeight = mediaNameCanvas.height;
-       var boxWidth = playBarHeight * 0.75;
+       playBarHeight = mediaNameCanvas.height;
+       boxWidth = playBarHeight * 0.75;
+       textStartX = boxWidth + 50;
+       songDurationSec = this.playerControls.duration;
        mediaNameCTX.clearRect(0,0,mediaNameCanvas.width, mediaNameCanvas.height);
 
-       var shadeJump = 10;
-       var alphaJump = 0.01;
-       var currColor = 255;
-       var currAlpha = 1.0;
-
        mediaNameCTX.fillStyle="rgba(30,30,30,0.5)";
        mediaNameCTX.strokeStyle="rgba(130,130,130,1)";
        mediaNameCTX.lineWidth = 5;
@@ -116,35 +145,74 @@ AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
 
        if (screenOrientation === "portrait")
        {
-               var textStartX = boxWidth + 50;
-
                var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 70 , "zLoc" : 0,
                                                        "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 65, "wordWrap" : true});
 
                trackText.applyTemplate(mainMenuTitleTemplate);
 
-               var artistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 70, "zLoc" : 0,
+               mainArtistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 70, "zLoc" : 0,
                                                                "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 50, "wordWrap" : true});
 
-               artistText.applyTemplate(mainTrackTemplate);
+               mainArtistText.applyTemplate(mainTrackTemplate);
        }
        else
        {
-               var textStartX = boxWidth + 50;
                var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 50 , "zLoc" : 0,
                                                                "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
                trackText.applyTemplate(mainMenuTitleTemplateLandscape);
-               var artistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
+               mainArtistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
                                                                "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
-               artistText.applyTemplate(mainTrackTemplateLandscape);
+               mainArtistText.applyTemplate(mainTrackTemplateLandscape);
        }
 
        trackText.drawObj();
        trackText.drawLargeShadow();
-       artistText.yLoc += trackText.height;
-       artistText.drawObj();
+       mainArtistText.yLoc += trackText.height;
+       mainArtistText.drawObj();
+
+       this.updateTrackTime();
 }
 
+/*
+ * Redraws the current track time
+ */
+
+AudioPlayer.prototype.updateTrackTime = function()
+{
+       playBarHeight = mediaNameCanvas.height;
+       boxWidth = playBarHeight * 0.75;
+       textStartX = boxWidth + 50;
+       var timeText = '';
+       var songDurationSec = Math.floor(this.playerControls.duration % 60) > 9 ? Math.floor(this.playerControls.duration % 60) : '0' + Math.floor(this.playerControls.duration % 60);
+       var songDurationMin = Math.floor(this.playerControls.duration / 60) > 9 ?  Math.floor(this.playerControls.duration / 60) : '0' +  Math.floor(this.playerControls.duration / 60);
+       var songCurrentMin = Math.floor(this.playerControls.currentTime / 60) > 9 ? Math.floor(this.playerControls.currentTime / 60) :  '0' + Math.floor(this.playerControls.currentTime / 60);
+       var songCurrentSec = Math.floor(this.playerControls.currentTime % 60) > 9 ? Math.floor(this.playerControls.currentTime % 60) : '0' + Math.floor(this.playerControls.currentTime % 60);
+
+       if ((songCurrentMin !== undefined && songCurrentSec !== undefined) && (!isNaN(songCurrentMin) && !isNaN(songCurrentSec)))
+               timeText += songCurrentMin + ':' + songCurrentSec + ' / ';
+       else
+               timeText += "00:00 / ";
+
+       if ((songDurationSec !== undefined && songDurationMin !== undefined) && (!isNaN(songDurationMin) && !isNaN(songDurationSec)))
+               timeText += songDurationMin + ':' + songDurationSec;
+       else
+               timeText += "00:00";
+
+
+       var trackTimeText = new TextObject(trackTimeCTX,{"text" : timeText, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
+                                                               "width" : mediaNameCanvas.width - textStartX, "height" : 20, "lineHeight" : 20, "wordWrap" : false});
+
+       trackTimeText.applyTemplate(timeTemplate);
+       trackTimeCTX.clearRect(0, 0, trackTimeCanvas.width, trackTimeCanvas.height);
+
+       trackTimeText.yLoc = ((playBarHeight - boxWidth) / 2) + boxWidth;
+       trackTimeText.drawObj();
+}
+
+/*
+ * Plays the currently loaded media file
+ */
+
 AudioPlayer.prototype.playLoadedMedia = function()
 {
        console.log("MediaPlayer in playLoadedMedia");
@@ -180,6 +248,9 @@ AudioPlayer.prototype.playLoadedMedia = function()
        this.currentFileLoaded = true;
 }
 
+/*
+ * Sort the file list alphabetically
+ */
 
 AudioPlayer.prototype.sortByAlpha = function()
 {
@@ -203,6 +274,10 @@ AudioPlayer.prototype.sortByAlpha = function()
        this.fillMediaList();
 }
 
+/*
+ * Sort the file list by artist name
+ */
+
 AudioPlayer.prototype.sortByArtist = function()
 {
        console.log("MediaPlayer in sortByArtist");
@@ -225,6 +300,10 @@ AudioPlayer.prototype.sortByArtist = function()
        this.fillMediaList();
 }
 
+/*
+ * Sort the file list by album name
+ */
+
 AudioPlayer.prototype.sortByAlbum =function()
 {
        console.log("MediaPlayer in sortByAlbum");
@@ -247,6 +326,10 @@ AudioPlayer.prototype.sortByAlbum =function()
        this.fillMediaList();
 }
 
+/*
+ * Callback for when the local content has been successfully read using tizen.content.
+ */
+
 AudioPlayer.prototype.onContentLoaded = function()
 {
        try
@@ -292,10 +375,9 @@ AudioPlayer.prototype.onContentLoaded = function()
        }
 }
 
-AudioPlayer.prototype.redrawMediaName = function()
-{
-       this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
-}
+/*
+ * Create and draw an item in the media list
+ */
 
 AudioPlayer.prototype.makeListItem = function(j, k)
 {
@@ -303,6 +385,9 @@ AudioPlayer.prototype.makeListItem = function(j, k)
 
     for (var i = j; (i < (j+k) && i < this.content.length); i++)
     {
+               if (typeof this.content[i].title !== "string")
+                       this.content[i].title = "Unknown";
+
            //Check if album art is done loading, if not draw default until it is
             if (this.content[i].artists === undefined)
             {
@@ -310,10 +395,13 @@ AudioPlayer.prototype.makeListItem = function(j, k)
                this.content[i].artists[0] = "Unknown";
             }
 
-            var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
-         var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
+            else if (typeof this.content[i].artists[0] !== "string")
+                       this.content[i].artists[0] = "Unknown";
+
+           var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
+           var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
 
-           if (this.content[i].coverArt && this.content[i].coverArt.complete)
+           if (this.content[i].coverArt && this.content[i].coverArt.complete && (this.content[i].coverArt.naturalWidth !== 0 || this.content[i].coverArt.naturalHeight !== 0) )
                this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
            else
                this.makeListBar(musicIcon, i, artistText, trackText);