-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicPlayer.java
More file actions
317 lines (291 loc) · 12.2 KB
/
MusicPlayer.java
File metadata and controls
317 lines (291 loc) · 12.2 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.control.TableView;
import javafx.scene.media.MediaPlayer;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.TextField;
import javafx.scene.Scene;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.media.Media;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.FXCollections;
import java.io.File;
import javafx.scene.control.Label;
import javafx.collections.MapChangeListener;
import java.io.IOException;
import java.util.ArrayList;
/**
* A Music Player Application!!!!
*@author eday30
*@version 1.0
*/
public class MusicPlayer extends Application {
private MediaPlayer player;
private TableView<Music> table;
private TableView<Music> searchTable;
private ObservableList<File> enumList = FXCollections.observableArrayList();
private ObservableList<Music> playerlist = FXCollections.
observableArrayList();
private ObservableList<Music> searchList = FXCollections.
observableArrayList();
@Override
public void start(Stage stage) throws IOException {
File s = new File(".");
File[] songFiles = s.listFiles();
String absPath = "file:" + s.getAbsolutePath();
ArrayList<Media> myMedia = new ArrayList<>();
for (int i = 0; i < songFiles.length; i++) {
if (songFiles[i].toString().contains("mp3")) {
myMedia.add(new Media(songFiles[i].toURI().toString()));
}
}
ArrayList<Music> myMusic = new ArrayList<>();
for (int i = 0; i < myMedia.size(); i++) {
myMusic.add(new Music(myMedia.get(i)));
}
for (Music m : myMusic) {
playerlist.add(m);
}
table = new TableView<Music>(playerlist);
table.setItems(playerlist);
table.setPrefWidth(900);
TableColumn<Music, String> firstColumn =
new TableColumn<Music, String>("File Name");
firstColumn.setCellValueFactory(
new PropertyValueFactory<Music, String>("fileName"));
firstColumn.setPrefWidth(450);
TableColumn<Music, String> secondColumn =
new TableColumn<Music, String>("Attributes");
secondColumn.setCellValueFactory(
new PropertyValueFactory<>("attributes"));
secondColumn.setPrefWidth(450);
TableColumn<Music, String> artistColumn =
new TableColumn<Music, String>("Artist");
artistColumn.setCellValueFactory(
new PropertyValueFactory<>("artist"));
artistColumn.setPrefWidth(150);
TableColumn<Music, String> titleColumn =
new TableColumn<Music, String>("Title");
titleColumn.setCellValueFactory(
new PropertyValueFactory<>("title"));
titleColumn.setPrefWidth(150);
TableColumn<Music, String> albumColumn =
new TableColumn<Music, String>("Album");
albumColumn.setCellValueFactory(new PropertyValueFactory<>("album"));
albumColumn.setPrefWidth(150);
secondColumn.getColumns().setAll(artistColumn, titleColumn,
albumColumn);
table.getColumns().addAll(firstColumn, secondColumn);
table.setEditable(true);
// MediaPlayer player = new MediaPlayer(table.getItems().get(3));
Button playButton = new Button("Play");
Button pauseButton = new Button("Pause");
Button searchButton = new Button("Search Songs");
Button showButton = new Button("Show all Songs");
pauseButton.setDisable(true);
showButton.setDisable(true);
playButton.setOnAction(e -> {
if (table.getSelectionModel().getSelectedItem() == null) {
player = new MediaPlayer(searchTable.getSelectionModel()
.getSelectedItem().getMedia());
} else {
player = new MediaPlayer(table.getSelectionModel()
.getSelectedItem().getMedia());
}
player.play();
player.setOnEndOfMedia(new Runnable() {
public void run() {
playButton.setDisable(false);
pauseButton.setDisable(true);
playButton.requestFocus();
}
});
playButton.setDisable(true);
pauseButton.setDisable(false);
pauseButton.requestFocus();
}
);
pauseButton.setOnAction(e -> {
player.pause();
pauseButton.setDisable(true);
playButton.setDisable(false);
playButton.requestFocus();
}
);
HBox buttonBox = new HBox();
buttonBox.getChildren().setAll(playButton, pauseButton, searchButton,
showButton);
VBox full = new VBox(table, buttonBox);
searchButton.setOnAction(e -> {
VBox labelBox = new VBox();
HBox searchBox = new HBox();
Label newLabel = new Label("Search for songs in Music Player");
TextField searchLine = new TextField("Search for a song," +
" album, artist, or title");
searchLine.setPrefWidth(250);
Button search = new Button("Search");
searchBox.getChildren().setAll(searchLine, search);
labelBox.getChildren().setAll(newLabel, searchBox);
searchBox.setPrefWidth(400);
searchBox.setPrefHeight(200);
search.setOnAction(a -> {
String searchText = searchLine.getText();
if (searchList.size() != 0) {
searchList.clear();
}
for (Music m : playerlist) {
if (m.getFileName().contains(searchText)) {
searchList.add(m);
} else if (m.getArtist() != null &&
m.getArtist().contains(searchText)) {
searchList.add(m);
} else if (m.getAlbum() != null &&
m.getAlbum().contains(searchText)) {
searchList.add(m);
} else if(m.getTitle() != null &&
m.getTitle().contains(searchText)) {
searchList.add(m);
}
}
searchTable = new TableView<>(searchList);
searchTable.setItems(searchList);
searchTable.setPrefWidth(900);
TableColumn<Music, String> searchFirstColumn =
new TableColumn<Music, String>("File Name");
searchFirstColumn.setCellValueFactory(
new PropertyValueFactory<Music, String>("fileName")
);
searchFirstColumn.setPrefWidth(450);
TableColumn<Music, String> searchSecondColumn =
new TableColumn<Music, String>("Attributes");
searchSecondColumn.setCellValueFactory(
new PropertyValueFactory<>("attributes"));
searchSecondColumn.setPrefWidth(450);
TableColumn<Music, String> searchArtistColumn =
new TableColumn<Music, String>("Artist");
searchArtistColumn.setCellValueFactory(
new PropertyValueFactory<>("artist"));
searchArtistColumn.setPrefWidth(150);
TableColumn<Music, String> searchTitleColumn =
new TableColumn<Music, String>("Title");
searchTitleColumn.setCellValueFactory(
new PropertyValueFactory<>("title"));
searchTitleColumn.setPrefWidth(150);
TableColumn<Music, String> searchAlbumColumn =
new TableColumn<Music, String>("Album");
searchAlbumColumn.setCellValueFactory(
new PropertyValueFactory<>("album"));
searchAlbumColumn.setPrefWidth(150);
searchSecondColumn.getColumns().setAll(
searchArtistColumn, searchTitleColumn,
searchAlbumColumn);
searchTable.getColumns().addAll(searchFirstColumn,
searchSecondColumn);
searchButton.setDisable(true);
showButton.setDisable(false);
showButton.requestFocus();
VBox searchStuff = new VBox(searchTable, buttonBox);
Scene searchScene = new Scene(searchStuff);
stage.setScene(searchScene);
stage.show();
}
);
Scene newWindowScene = new Scene(labelBox, 400, 200);
Stage searchStage = new Stage();
searchStage.setTitle("Search Songs");
searchStage.setScene(newWindowScene);
searchStage.show();
}
);
Scene scene = new Scene(full);
stage.setScene(scene);
showButton.setOnAction(b -> {
VBox showBox = new VBox(table, buttonBox);
Scene showScene = new Scene(showBox);
showButton.setDisable(true);
searchButton.setDisable(false);
searchButton.requestFocus();
stage.setScene(showScene);
stage.show();
}
);
stage.setTitle("Music Player");
stage.show();
}
/**
* A Music Helper Class
*@author eday30
*@version 1.0
*/
public class Music {
private String fileName;
private Media media;
private String artist;
private String title;
private String album;
/**
* A Music Constructor. Converts Media Object to Music Object to extract
* attributes.
*@param m is the Media Object
*/
public Music(Media m) {
ObservableMap<String, Object> mapData = m.getMetadata();
fileName = m.getSource();
media = m;
mapData.addListener(new MapChangeListener<String, Object>() {
public void onChanged(MapChangeListener.Change<? extends String,
? extends Object> change) {
artist = (String) mapData.get("artist");
title = (String) mapData.get("title");
album = (String) mapData.get("album");
table.refresh();
}
});
}
/**
* A getter for the Artist
*@return artist is the String of the artist name
*/
public String getArtist() {
return artist;
}
/**
* A getter for title
*@return title is the String of the title
*/
public String getTitle() {
return title;
}
/**
*A getter for album
*@return album is the String of the album
*/
public String getAlbum() {
return album;
}
/**
* A getter for the File Name
*@return finish is a String of the corrected File Name
*/
public String getFileName() {
String start = fileName;
int index = start.lastIndexOf('/');
StringBuilder sb = new StringBuilder(fileName);
sb.delete(0, index + 1);
String finish = sb.toString();
finish = finish.replace("%20", " ");
return finish;
}
/**
* A getter for the Media Object
*@return media is the Object that is retained post-Music Transformation
*/
public Media getMedia() {
return media;
}
}
}