-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSong.java
More file actions
69 lines (59 loc) · 1.95 KB
/
Copy pathSong.java
File metadata and controls
69 lines (59 loc) · 1.95 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
import javax.sound.sampled.*;
import java.io.File;
public class Song extends Media {
private Clip clip;
private FloatControl volumeControl;
public Song(String title, String filePath) {
super(title, filePath);
}
@Override
public void play() {
try {
if (clip != null && clip.isOpen()) {
clip.start();
} else {
File file = new File(filePath);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioStream);
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
}
clip.start();
}
} catch (Exception e) {
System.err.println("Oynatma hatası: " + e.getMessage());
}
}
@Override
public void stop() {
if (clip != null && clip.isRunning()) clip.stop();
}
@Override
public void setPosition(int seconds) {
if (clip != null && clip.isOpen()) {
clip.setFramePosition(0); // Önce sıfırla (bazı sistemlerde gerekli)
clip.setMicrosecondPosition(seconds * 1_000_000L);
}
}
@Override
public void setVolume(float volume) {
if (volumeControl != null) {
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
volumeControl.setValue(min + (max - min) * volume);
}
}
@Override
public long getTotalSeconds() {
return (clip != null) ? clip.getMicrosecondLength() / 1_000_000L : 0;
}
@Override
public long getCurrentSeconds() {
return (clip != null) ? clip.getMicrosecondPosition() / 1_000_000L : 0;
}
@Override
public boolean isPlaying() {
return clip != null && clip.isRunning();
}
}