-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicPlayerGUI.java
More file actions
147 lines (124 loc) · 5.38 KB
/
Copy pathMusicPlayerGUI.java
File metadata and controls
147 lines (124 loc) · 5.38 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MusicPlayerGUI extends JFrame {
private List<Media> playlist = new ArrayList<>();
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> songListDisplay;
private JSlider progressSlider, volumeSlider;
private JLabel currentTimeLabel, totalTimeLabel;
private int currentSongIndex = -1;
private boolean isUserChangingSlider = false;
public MusicPlayerGUI() {
setTitle("Java Music Player");
setSize(500, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
// --- ŞARKI LİSTESİ ---
loadSongsFromFolder("songs");
songListDisplay = new JList<>(listModel);
add(new JScrollPane(songListDisplay), BorderLayout.CENTER);
// --- KONTROL PANELİ ---
JPanel southPanel = new JPanel();
southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
// 1. İlerleme Çubuğu (Spotify Style)
JPanel progressPanel = new JPanel(new BorderLayout(5, 0));
currentTimeLabel = new JLabel("00:00");
totalTimeLabel = new JLabel("00:00");
progressSlider = new JSlider(0, 100, 0);
progressPanel.add(currentTimeLabel, BorderLayout.WEST);
progressPanel.add(progressSlider, BorderLayout.CENTER);
progressPanel.add(totalTimeLabel, BorderLayout.EAST);
// 2. Butonlar
JPanel buttonPanel = new JPanel();
JButton playBtn = new JButton("▶");
JButton stopBtn = new JButton("■");
JButton skipBtn = new JButton("⏭");
buttonPanel.add(playBtn);
buttonPanel.add(stopBtn);
buttonPanel.add(skipBtn);
// 3. Ses Ayarı
JPanel volumePanel = new JPanel();
volumeSlider = new JSlider(0, 100, 70);
volumePanel.add(new JLabel("Vol:"));
volumePanel.add(volumeSlider);
// İSİM ETİKETİ
JLabel nameLabel = new JLabel("Made by: Ceyhun KEKLIK (TrezmikiS) ");
nameLabel.setFont(new Font("Arial", Font.ITALIC, 10));
nameLabel.setForeground(Color.GRAY);
nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
southPanel.add(progressPanel);
southPanel.add(buttonPanel);
southPanel.add(volumePanel);
southPanel.add(nameLabel);
add(southPanel, BorderLayout.SOUTH);
// --- EVENTLER ---
playBtn.addActionListener(e -> playSelected());
stopBtn.addActionListener(e -> { if(currentSongIndex != -1) playlist.get(currentSongIndex).stop(); });
skipBtn.addActionListener(e -> skipSong());
// Slider'a elle müdahale
progressSlider.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) { isUserChangingSlider = true; }
@Override public void mouseReleased(MouseEvent e) {
if (currentSongIndex != -1) {
playlist.get(currentSongIndex).setPosition(progressSlider.getValue());
}
isUserChangingSlider = false;
}
});
// Ses değişimi
volumeSlider.addChangeListener(e -> {
if (currentSongIndex != -1) {
playlist.get(currentSongIndex).setVolume(volumeSlider.getValue() / 100f);
}
});
// Arayüzü güncelleyen Timer
Timer timer = new Timer(500, e -> updateUI());
timer.start();
}
private void playSelected() {
int index = songListDisplay.getSelectedIndex();
if (index != -1) {
if (currentSongIndex != -1) playlist.get(currentSongIndex).stop();
currentSongIndex = index;
Media current = playlist.get(currentSongIndex);
current.play();
current.setVolume(volumeSlider.getValue() / 100f);
}
}
private void skipSong() {
if (playlist.isEmpty()) return;
if (currentSongIndex != -1) playlist.get(currentSongIndex).stop();
currentSongIndex = (currentSongIndex + 1) % playlist.size();
songListDisplay.setSelectedIndex(currentSongIndex);
playSelected();
}
private void updateUI() {
if (currentSongIndex != -1 && playlist.get(currentSongIndex).isPlaying() && !isUserChangingSlider) {
Media current = playlist.get(currentSongIndex);
long curSec = current.getCurrentSeconds();
long totalSec = current.getTotalSeconds();
progressSlider.setMaximum((int) totalSec);
progressSlider.setValue((int) curSec);
currentTimeLabel.setText(current.formatTime(curSec));
totalTimeLabel.setText(current.formatTime(totalSec));
}
}
private void loadSongsFromFolder(String folderPath) {
File folder = new File(folderPath);
if (!folder.exists()) folder.mkdir(); // Klasör yoksa oluştur
File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".wav"));
if (files != null) {
for (File f : files) {
playlist.add(new Song(f.getName(), f.getPath()));
listModel.addElement(f.getName());
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MusicPlayerGUI().setVisible(true));
}
}