This repository was archived by the owner on Jul 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundPlayerGUI.java
More file actions
263 lines (219 loc) · 8.31 KB
/
SoundPlayerGUI.java
File metadata and controls
263 lines (219 loc) · 8.31 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;
/**
* A simple sound player. To start, create an instance of this class.
*
* The sound player can play sound clips in WAV, AU and AIFF formats
* with standard sample rates.
*
* @author Michael Kolling and David J Barnes
* @version 1.0
*/
public class SoundPlayerGUI extends JFrame
implements ListSelectionListener
{
private static final String VERSION = "Version 1.0";
private JList fileList;
private JLabel infoLabel;
private Player player;
/**
* Create a SoundPlayer and display its GUI on screen.
*/
public SoundPlayerGUI()
{
super("KOS Sound");
player = new Player();
PlayList tracks = player.getPlayList();
makeFrame(tracks);
}
/**
* Play the sound file currently selected in the file list. If there is no
* selection in the list, or if the selected file is not a sound file,
* do nothing.
*/
private void play()
{
player.play();
}
/**
* Display information about a selected sound file (name and clip length).
* @param message The message to display.
*/
private void showInfo(String message)
{
infoLabel.setText(message);
}
/**
* Stop the currently playing sound file (if there is one playing).
*/
private void stop()
{
player.stop();
}
/**
* Quit function: quit the application.
*/
private void quit()
{
System.exit(0);
}
/**
* About function: show the 'about' box.
*/
private void showAbout()
{
String text = "kSound\n" + VERSION + "\n"
+ "Total tracks played: " + player.getNumberOfTracksPlayed() + "\n"
+ "Total track time: " + player.getTotalPlayedTrackLength() + "\n"
+ "Average track time: " + player.averageTrackLength();
JOptionPane.showMessageDialog(this,
text,
"About SoundPlayer",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Statistics: Show information about tracks played, total play time and avg play time,
* with ability to reset values to zero.
*/
private void statistics(){
String stat = "Statistics:\n\nTracks played: " + player.getNumberOfTracksPlayed() + "\n"
+ "Total track time: " + player.getTotalPlayedTrackLength() + "\n"
+ "Average track time: " + player.averageTrackLength();
Object[] options = {"Close", "Reset"};
// JOptionPane.showMessageDialog(this, stat, "Statistics", JOptionPane.INFORMATION_MESSAGE);
int n = JOptionPane.showOptionDialog(this, stat, "Statistics", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
// If button Reset is pressed, a dialog is displayed promting the user with a yes/no option. Yes will reset the statistics.
// Method statistics() is called to view the statistics.
if(n == 1){
Object[] optionYesNo = {"Yes", "No"};
int m = JOptionPane.showOptionDialog(this, "Are you sure you want to reset statistics?","Reset", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, optionYesNo, optionYesNo[1]);
if(m == 0){
player.resetStatistics();
statistics();
}else{
statistics();
}
}
}
// --- ListSelectionListener interface ---
/**
* List selection changed. Called when the user select an entry in the track list.
*/
public void valueChanged(ListSelectionEvent evt)
{
int selected = fileList.getSelectedIndex();
if(selected != -1) {
player.setTrack(selected);
}
showInfo(player.getTrackInfo());
}
// ---- Swing stuff to build the frame and all its components and menus ----
/**
* Create the complete application GUI.
* @param audioFiles The file names to display.
*/
private void makeFrame(PlayList tracks)
{
// the following makes sure that our application exits when
// the user closes its window
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel contentPane = (JPanel)getContentPane();
contentPane.setBorder(new EmptyBorder(6, 10, 10, 10));
makeMenuBar();
// Specify the layout manager with nice spacing
contentPane.setLayout(new BorderLayout(8, 8));
// Create the left side with combobox and scroll list
JPanel leftPane = new JPanel();
{
leftPane.setLayout(new BorderLayout(8, 8));
// Create the scrolled list for file names
fileList = new JList(tracks.asStrings());
fileList.setForeground(new Color(212,212,255));
fileList.setBackground(new Color(0,85,150));
fileList.setSelectionBackground(new Color(212,212,255));
fileList.setSelectionForeground(new Color(0,45,75));
fileList.addListSelectionListener(this);
JScrollPane scrollPane = new JScrollPane(fileList);
scrollPane.setColumnHeaderView(new JLabel("Tracks"));
leftPane.add(scrollPane, BorderLayout.CENTER);
}
contentPane.add(leftPane, BorderLayout.CENTER);
// Create the center with image, text label, and slider
JPanel centerPane = new JPanel();
{
centerPane.setLayout(new BorderLayout(8, 8));
JLabel image = new JLabel(new ImageIcon("title.jpg"));
centerPane.add(image, BorderLayout.NORTH);
centerPane.setBackground(Color.WHITE);
infoLabel = new JLabel("No track is selected");
infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
infoLabel.setForeground(new Color(0,85,150));
centerPane.add(infoLabel, BorderLayout.CENTER);
centerPane.add(new JLabel(" "),BorderLayout.SOUTH);
}
contentPane.add(centerPane, BorderLayout.EAST);
// Create the toolbar with the buttons
JPanel toolbar = new JPanel();
{
toolbar.setLayout(new GridLayout(1, 0));
JButton button = new JButton("Play");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { play(); }
});
toolbar.add(button);
button = new JButton("Stop");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { stop(); }
});
toolbar.add(button);
button = new JButton("Statistics");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { statistics(); }
});
toolbar.add(button);
contentPane.add(toolbar, BorderLayout.NORTH);
// building is done - arrange the components
pack();
// place this frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
setVisible(true);
}
}
/**
* Create the main frame's menu bar.
*/
private void makeMenuBar()
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
// create the File menu
menu = new JMenu("File");
menubar.add(menu);
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { quit(); }
});
menu.add(item);
// create the Help menu
menu = new JMenu("Help");
menubar.add(menu);
item = new JMenuItem("About SoundPlayer...");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { showAbout(); }
});
menu.add(item);
}
}