Initial package
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / MediaPlayer / project / js / videoPlayer.js
1
2 /*
3  * Copyright (c) 2013, Intel Corporation.
4  *
5  * This program is licensed under the terms and conditions of the
6  * Apache License, version 2.0.  The full text of the Apache License is at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  */
10
11 VideoPlayer = function()
12 {
13         this.clearVideoTimeInterval = undefined;
14         this.loadPrevVideo = false;
15         this.currentVideoContent = undefined;
16 }
17
18 VideoPlayer.prototype.play = function()
19 {
20         console.log("MediaPlayer in VideoPlayer::play");
21         if (this.playerControls.paused)
22         {
23                 this.playerControls.play();
24
25                 //Start tracking the current time of the media.  This is used to play from previous position on restart.
26                 if (!this.content[this.listIndex].remoteFile)
27                 {
28                         var timeoutFunction = function()
29                         {
30                                 localStorage.prevVideoTime = this.playerControls.currentTime;
31                         }
32
33                         //Start tracking the current time of the media.  This is used to play from previous position on restart.
34                         this.clearVideoTimeInterval = setInterval(timeoutFunction.bind(this),500);
35                 }
36         }
37 }
38
39 VideoPlayer.prototype.pause = function()
40 {
41         if (!this.playerControls.paused)
42         {
43                 this.playerControls.pause();
44                 clearInterval(this.clearVideoTimeInterval);
45         }
46 }
47
48 VideoPlayer.prototype.playing = function()
49 {
50         return !(this.playerControls.paused);
51 }
52
53 VideoPlayer.prototype.next = function()
54 {
55         if (this.content)
56         {
57                 if (this.content.length > (this.listIndex + 1))
58                         this.listIndex++;
59                 else
60                         this.listIndex = 0;
61         this.load(this.listIndex, false);
62         }
63 }
64
65 VideoPlayer.prototype.previous = function()
66 {
67         if (this.content)
68         {
69                 if (this.listIndex > 0 )
70                         this.listIndex--;
71                 else
72                         this.listIndex = this.content.length - 1;
73
74         this.load(this.listIndex, false);
75         }
76 }
77
78 VideoPlayer.prototype.load = function(index, play)
79 {
80         this.listIndex = index;
81         this.loadAndPlay = play;
82         this.playerControls.pause();
83         $("#videoSrc").attr("src", this.content[this.listIndex].contentURI);
84         this.playerControls.load();
85 }
86
87 VideoPlayer.prototype.videoLoaded = function()
88 {
89         this.currentVideoContent = this.content[this.listIndex];
90
91         //Only store track to localStorage if it's a local file, and current menu is the Video menu
92         if (!this.content[this.listIndex].remoteFile && localStorage.prevMenu === "mainVideoButton")
93         {
94                 localStorage.prevVideo = this.content[this.listIndex].contentURI;
95         }
96         else
97         {
98                 localStorage.prevVideo = undefined;
99                 localStorage.prevVideoTime = undefined;
100         }
101
102         if (this.loadPrevVideo)
103         {
104                 this.playerControls.currentTime = localStorage.prevVideoTime;
105                 this.loadPrevVideo = false;
106                 this.play();
107         }
108
109         if (this.loadAndPlay)
110                 this.play();
111
112         this.currentFileLoaded = true;
113 }
114
115 VideoPlayer.prototype.onContentLoaded = function()
116 {
117         try
118         {
119                 if (localStorage.prevVideo && localStorage.prevVideo !== "undefined")
120                 {
121                         for (var i = 0; i < this.content.length; i++)
122                         {
123                                 if (this.content[i].contentURI === localStorage.prevVideo)
124                                 {
125                                         console.log("MediaPlayer: Previous video found, loading " + this.content[i].contentURI);
126                                         this.listIndex = i;
127                                         this.loadPrevVideo = true;
128                                         this.fillMediaList();
129                                         this.load(i, true);
130                                 }
131                         }
132                 }
133                 else
134                 {
135                         console.log("MediaPlayer: No previous video found, loading first");
136                         this.load(0,false);
137                 }
138         }
139
140         catch (err)
141         {
142                 console.log("MediaPlayer: Error when parsing videoContent");
143         }
144 }
145
146 VideoPlayer.prototype.makeListItem = function(j, k)
147 {
148         var canvasH = mediaListItemH * 0.95 ;
149
150         for (var i = j; (i < (j+k) && i < this.content.length); i++)
151         {
152                 var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
153                 var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
154
155                 this.makeListBar(vidIcon, i, artistText, trackText);
156
157                 // Set callback function for the new list item
158                 $("#" + this.type + "CanvasNum" + i).click(function () {
159                                 try
160                                 {
161                                         videoPlayer.load($(this).parent().parent().index(), true);
162                                         $("#videoMediaList").hide();
163                                 }
164                                 catch(err)
165                                 {
166                                         console.log("MediaPlayer: load video error " + err.message);
167                                 }
168                         });
169         }
170 }