-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaGUIMusicPlayerJFrame.java
More file actions
184 lines (153 loc) · 6 KB
/
Copy pathJavaGUIMusicPlayerJFrame.java
File metadata and controls
184 lines (153 loc) · 6 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
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*; //UI
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class JavaGUIMusicPlayerJFrame extends JFrame implements ActionListener {
private JProgressBar progressBar;
private JLabel timeLabel;
private JTextField filePathField;
private JButton playButton;
private JButton pauseButton;
private JButton chooseButton;
private JButton loopButton;
private boolean isPaused;
private boolean isLooping = false;
private JFileChooser fileChooser;
private Clip clip;
public JavaGUIMusicPlayerJFrame(){
super("Music Player Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
progressBar= new JProgressBar();
filePathField = new JTextField(20);
timeLabel = new JLabel("00:00 / 00:00");
playButton = new JButton("play");
pauseButton = new JButton("pause");
chooseButton = new JButton("Choose file");
loopButton = new JButton("loop");
isPaused = false;
isLooping = false;
//for each button, add the ability to detect that it is being clicked by adding an 'action listener'
playButton.addActionListener(this);
pauseButton.addActionListener(this);
chooseButton.addActionListener(this);
loopButton.addActionListener(this);
//adding a textfield to the JFrame so it is visible
add(progressBar);
add(filePathField);
add(chooseButton);
add(playButton);
add(pauseButton);
add(loopButton);
add(timeLabel);
//set the default file path of the JFilechooser to be what the directory of the project
fileChooser = new JFileChooser(".");
fileChooser.setFileFilter(new FileNameExtensionFilter("WAV Files", "wav"));
//setting the window options
setSize(500,150); //size of the window
setLocationRelativeTo(null); //position of the jframe window on screen
setVisible(true); //whether you can see the window or not (i.e it is hidden)
//setting up the progress bar
progressBar.setValue(0);
progressBar.setBounds(0,0,500, 150);
}
@Override
public void actionPerformed(ActionEvent event){
if(event.getSource() == playButton){ playMusic(); }
else if (event.getSource()== pauseButton){ pauseMusic(); }
else if(event.getSource() == chooseButton){ chooseFile(); }
else if (event.getSource()== loopButton){ toggleLoop(); }
}
public void playMusic() {
//if we're playing music, we want the music to stop
if (clip != null && clip.isRunning()) {
clip.stop();
}
try{
File file = new File(filePathField.getText());
AudioInputStream audioInput = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioInput);
if(isLooping){
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
clip.start();
startProgressBar();
}
catch(Exception e){
System.out.println(e);
}
}
public void pauseMusic() {
if (clip != null && clip.isRunning()) {
clip.stop();
isPaused = true;
pauseButton.setText("Resume");
} else if (clip != null && isPaused) {
clip.start();
startProgressBar();
if (isLooping) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
isPaused = false;
pauseButton.setText("Pause");
}
}
public void chooseFile() {
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
filePathField.setText(selectedFile.getAbsolutePath());
}
}
public void toggleLoop () {
isLooping = !isLooping;
if (isLooping) {
loopButton.setText("Stop Loop");
if (clip.isRunning()) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
} else {
loopButton.setText("Loop");
if (clip.isRunning()) {
clip.loop(0);
}
}
}
//implementing a progress bar into the program, call it in the playMusic metho
public void startProgressBar() {
new Thread(() -> {
try {
while (clip != null) {
long current = clip.getMicrosecondPosition();
long total = clip.getMicrosecondLength();
long positionInLoop = current % total;
int progress = (int) ((positionInLoop * 100) / total);
progressBar.setValue(progress);
Thread.sleep(1000);
long totalLengthInMicros = clip.getMicrosecondLength();
long totalLength = totalLengthInMicros/1_000_000;
long currentSecond = positionInLoop/1_000_000;
//get to display minute:second (ie 01:23)
long currentMinuteCount = currentSecond/60;
long currentSecondCount = currentSecond % 60;
long totalMinuteCount = totalLength / 60;
long totalSecondCount = totalLength % 60;
String timeText = String.format("%02d:%02d / %02d:%02d", currentMinuteCount, currentSecondCount, totalMinuteCount, totalSecondCount);
timeLabel.setText(timeText);
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
public static void main (String[]args){
new JavaGUIMusicPlayerJFrame();
}
}