]> Joshua Wise's Git repositories - patchfork.git/blob - std/streaming.js
Update support for Amazon coverart grabbing (patch from Václav Nováček <waclaw66...
[patchfork.git] / std / streaming.js
1 /* 
2     Pitchfork Music Player Daemon Client
3     Copyright (C) 2007  Roger Bystrøm
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; version 2 of the License.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 var streaming_info = null;
20
21 function StreamingInfo() {
22         this.open = false;
23         this.display = null;
24         var c = setting_get("sap");
25         if(c==null) {
26                 this.auto_play = true;
27                 setting_set("sap", "true");
28         }
29         else {
30                 this.auto_play = c=="true"?true:false;
31         }
32         this.auto_play_node = null;
33         this.applet = null;
34         this.notification_txt = null;
35         this.check_delay = 0.3 * 1000;
36         this.eventListeners = new Array();
37 }
38
39 function streaming_init() {
40         streaming_info = new StreamingInfo();
41 }
42
43 function streaming_destroy() {
44         while(streaming_info.eventListeners.length)
45                 streaming_info.eventListeners.pop().unregister();
46         
47         remove_node(streaming_info.applet);
48         remove_node(streaming_info.auto_play_node);
49         remove_node(streaming_info.notification_txt);
50         remove_node(streaming_info.display);
51
52         streaming_info.applet = null;
53         streaming_info.auto_play_node = null;
54         streaming_info.notification_txt = null;
55         streaming_info.display = null;
56
57         streaming_info.open = false;
58 }
59
60 function streaming_open(e) {
61         var s =streaming_info.display; 
62         if(!s) {
63                 s = create_node("div", "streaming_display");
64                 var d2 = create_node("div");
65                 d2.style.marginLeft = "5px";
66                 if(!window.SHOUT_URL)
67                         return;
68                 /* if someone can make this work in all browsers and not crash/hang
69                    anyone of them I'd like to know... */
70
71                 function make_item(txt) {
72                         var item = create_node("li");
73                         item.className = "fakelink";
74                         item.style.borderLeft = item.style.borderRight = "none";
75                         add_txt(item, txt);
76                         return item;
77                 }
78
79                 var obj = "<applet type='application/x-java-applet'" +
80                           " width='70'" + 
81                           " height='32'" +
82                           " id='streamplayer'" + 
83                           " style='display: inline; visibility: hidden; position: absolute;'" +
84                           " archive='../jorbis/jorbis-pitchfork.jar'" +
85                           " classid='java:JOrbisPlayer.class'" +
86                           " code='JOrbisPlayer.class'" +
87                           "<param name='archive' value='../jorbis/jorbis-pitchfork.jar' />" +
88                           (streaming_info.auto_play?"<param name='jorbis.player.playonstartup' value='yes' />":"") +
89                           "<param name='jorbis.player.play.0' value='"+SHOUT_URL+"' />" +
90                           "<param name='jorbis.player.bgcolor' value='" + IMAGE.STREAM_BGCOLOR + "' />" + 
91                           "</applet>";
92                 d2.innerHTML = obj;
93                 s.appendChild(d2);
94                 var txt = create_node("ul");
95                 //txt.className = "fakelink";
96                 txt.className = "nomargin";
97                 //txt.style.margin = "0px 2px 0px 0px";
98                 txt.style.padding = "0px 0px 0px 0px";
99                 txt.style.fontSize = "smaller";
100
101                 var sp = create_node("span");
102                 sp.style.fontWeight = "bold";
103                 sp.style.padding = "5px 0px 5px 0px";
104                 add_txt(sp, LANG.STREAMING);
105                 txt.appendChild(sp);
106
107                 var item = make_item(LANG.CLOSE);
108                 streaming_info.eventListeners.push(add_listener(item, "click", streaming_destroy));
109                 txt.appendChild(item);
110
111                 item = make_item(LANG.HIDE);
112                 streaming_info.eventListeners.push(add_listener(item, "click", streaming_hide));
113                 txt.appendChild(item);
114                 
115                 item = make_item( streaming_info.auto_play?LANG.AUTOPLAY:LANG.NO_AUTOPLAY );
116                 txt.appendChild(item);
117                 streaming_info.eventListeners.push(add_listener(item, "click", streaming_toggle_auto_play));
118                 streaming_info.auto_play_node = item;
119
120                 item = make_item("");
121                 streaming_info.notification_txt = item;
122                 txt.appendChild(streaming_info.notification_txt);
123                 streaming_info.eventListeners.push(add_listener(streaming_info.notification_txt, "click", streaming_toggle_event));
124
125                 // insert container first in area
126                 insert_first(txt, s);
127
128                 document.getElementById('player_control').appendChild(s);
129                 streaming_info.display = s;
130                 streaming_info.applet = document.applets['streamplayer'];
131
132                 streaming_check_playing();
133                 document.body.focus();
134         }
135         if(streaming_info.open) {
136                 streaming_hide(e);
137         }
138         else {
139                 s.style.visibility = "";
140                 streaming_info.open = true;
141                 streaming_try_autoplay();
142                 if(e) {
143                         stop_event(e);
144                 }
145         }
146 }
147
148 /* hides the whole streaming area */
149 function streaming_hide(e) {
150         if(streaming_info.display) {
151                 streaming_info.display.style.visibility = "hidden";
152         }
153         streaming_info.open = false;
154         if(e) {
155                 stop_event(e);
156         }
157 }
158
159 /* toggles the autoplay feature */
160 function streaming_toggle_auto_play(e) {
161         if(e) stop_event(e);
162         if(streaming_info.auto_play_node) {
163                 var s = streaming_info.auto_play_node;
164                 remove_children(s);
165                 streaming_info.auto_play = !streaming_info.auto_play;
166                 add_txt(s, streaming_info.auto_play?LANG.AUTOPLAY:LANG.NO_AUTOPLAY);
167                 setting_set("sap", streaming_info.auto_play?"true":"false");
168         }
169 }
170
171 /* checks whether the applet is currently streaming or not, 
172  * returns false on error or non-existing applet */
173 function streaming_is_playing() {
174         if(streaming_info.applet) {
175                 try {
176                         return streaming_info.applet.isPlaying();
177                 } catch(e) { } 
178         }
179         return false;
180 }
181
182 /* tries to start playback if the applet is available */
183 function streaming_try_play() {
184         if(streaming_info.applet) {
185                 try {
186                         streaming_info.applet.play_sound();
187                 } catch(e) { }
188         }
189 }
190
191 /* tries to stop playback if the applet is available */
192 function streaming_try_stop() {
193         if(streaming_info.applet) {
194                 try {
195                         streaming_info.applet.stop_sound();
196                 } catch(e) { }
197         }
198 }
199
200 /* tries to start playing if autoplay is enabled */
201 function streaming_try_autoplay() {
202         if(streaming_info.auto_play&&streaming_info.display&&streaming_info.applet) {
203                 streaming_try_play();
204         }
205 }
206
207 /* tries to stop the audio playback if autoplay is enabled */
208 function streaming_try_autostop() {
209         if(streaming_info.auto_play&&streaming_info.display) {
210                 streaming_try_stop();
211         }
212 }
213
214 function streaming_update_stat() {
215         remove_children(streaming_info.notification_txt);
216         streaming_info.notification_txt.appendChild(create_txt(streaming_info.stat?LANG.STOP:LANG.PLAY));
217 }
218
219 function streaming_check_playing() {
220         var stat = streaming_is_playing();
221         if(streaming_info.stat != stat) {
222                 streaming_info.stat = stat;
223                 streaming_update_stat();
224         }
225         setTimeout(streaming_check_playing, streaming_info.check_delay);
226 }
227 function streaming_toggle_event(e) {
228         if(e) stop_event(e);
229         if(!streaming_is_playing()) {
230                 streaming_try_play();
231         } else {
232                 streaming_try_stop();
233         }
234 }
This page took 0.033906 seconds and 4 git commands to generate.