My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.gwtsite.musicplayer.client;

import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.SoundController;
import com.allen_sauer.gwt.voices.client.handler.SoundCompleteEvent;
import com.allen_sauer.gwt.voices.client.handler.SoundHandler;
import com.allen_sauer.gwt.voices.client.handler.SoundLoadStateChangeEvent;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.HttpProxy;
import com.gwtext.client.data.JsonReader;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;

/*
* Copyright 2008 Chris Fong
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
public class MusicPlayer implements EntryPoint {
private GridPanel grid;
private Button stopSongBtn;
private Sound currentSong;
private SoundController soundController = new SoundController();
private Panel lyricsArea;
private HTML nowPlaying;

public void onModuleLoad() {

buildSongGrid();
buildLyricsArea();

RootPanel.setVisible(DOM.getElementById("loading-indicator"), false);

VerticalPanel mainPanel = new VerticalPanel();
mainPanel.addStyleName("mainPanel");

HorizontalPanel topArea = new HorizontalPanel();
topArea.addStyleName("topArea");
topArea.add(grid);
topArea.add(lyricsArea);

nowPlaying = new HTML();
nowPlaying.addStyleName("nowPlaying");
nowPlaying.setText("Double-click on a song to play");
nowPlaying.setWidth("320px");

stopSongBtn = new Button();
stopSongBtn.setText("Stop song");
stopSongBtn.setVisible(false);
stopSongBtn.addStyleName("stopSongBtn");

mainPanel.add(topArea);
mainPanel.add(nowPlaying);
mainPanel.add(stopSongBtn);

RootPanel.get("content").add(mainPanel);
addListeners();

}

private void buildLyricsArea() {
lyricsArea = new Panel();
lyricsArea.setWidth("300px");
lyricsArea.setHeight("325px");
lyricsArea.setTitle("Lyrics");
lyricsArea.setCollapsible(true);
lyricsArea.setVisible(false);
lyricsArea.setAutoScroll(true);
lyricsArea.addStyleName("lyrics");
}

private void buildSongGrid() {

RecordDef recordDef = new RecordDef(
new FieldDef[] {
new StringFieldDef("title"),
new StringFieldDef("album")
}
);

HttpProxy dataProxy = new HttpProxy("songs.json");
JsonReader reader = new JsonReader(recordDef);
reader.setRoot("data");

Store store = new Store(dataProxy, reader);
store.load();

grid = new GridPanel();
grid.setStore(store);

ColumnConfig[] columns = new ColumnConfig[]{
new ColumnConfig("Song Title", "title", 160, true, null, "title"),
new ColumnConfig("Album", "album", 160)
};

ColumnModel columnModel = new ColumnModel(columns);
grid.setColumnModel(columnModel);

grid.setFrame(true);
grid.setStripeRows(true);
grid.setAutoExpandColumn("title");
grid.setWidth(320);
grid.setHeight(350);
grid.setTitle("Jonathan Coulton Songs");

}

private void addListeners() {
grid.addGridRowListener(new GridRowListenerAdapter() {

public void onRowClick(GridPanel grid, int rowIndex, EventObject e) {
Record record = grid.getStore().getAt(rowIndex);
String title = record.getAsString("title");
lyricsArea.setVisible(true);
lyricsArea.setTitle("Lyrics for " + title);
displaySongLyrics(title);
}

private void displaySongLyrics(String title) {
try
{
RequestBuilder request = new RequestBuilder(RequestBuilder.GET, title+".txt");
request.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
System.out.println("doh, error: " + exception.getStackTrace());
}
public void onResponseReceived(Request request, Response response) {
lyricsArea.setHtml(response.getText());
}

});
}
catch (RequestException e)
{
// unable to fetch lyrics
e.printStackTrace();
}
}

public void onRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
if (currentSong != null) {
currentSong.stop();
}

Record record = grid.getStore().getAt(rowIndex);
final String title = record.getAsString("title");
String mp3Name = title + ".mp3";

currentSong = soundController.createSound(Sound.MIME_TYPE_AUDIO_MPEG,
mp3Name);


currentSong.addEventHandler(new SoundHandler() {
public void onSoundComplete(SoundCompleteEvent event) {
songPlaying(false);
}

public void onSoundLoadStateChange(SoundLoadStateChangeEvent event) {
nowPlaying.setText("Now Playing - "+ title);
}

});
currentSong.play();
nowPlaying.setHTML("<img class=\"loading-img\" src=\"1-0.gif\"/>Loading song - "+ title);
songPlaying(true);
}

});

stopSongBtn.addListener(new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
currentSong.stop();
songPlaying(false);
}
});

}

private void songPlaying(boolean playing)
{
stopSongBtn.setVisible(playing);
nowPlaying.setVisible(playing);
}
}
Show details Hide details

Change log

r21 by fongcn on Mar 06, 2008   Diff
add loading indicators
Go to: 
Project members, sign in to write a code review

Older revisions

r20 by fongcn on Mar 05, 2008   Diff
[No log message]
r19 by fongcn on Mar 05, 2008   Diff
stop showing nowplaying label when
song stops
r17 by fongcn on Mar 05, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 6613 bytes, 216 lines
Hosted by Google Code