-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTMP3.java
More file actions
164 lines (148 loc) · 3.99 KB
/
TMP3.java
File metadata and controls
164 lines (148 loc) · 3.99 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
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class TMP3 extends Thread {
private AudioFormat decodedFormat;
private AudioInputStream in;
private AudioInputStream din;
private SourceDataLine line;
private File file;
String name;
private boolean end_flag = false;
private boolean pause_flag = false;
private boolean playing_flag = false;
public TMP3(String filename, String title) {
name = "";
try {
file = new File(filename);
if((title+".mp3").contains(file.getName())) name = file.getName().substring(0,file.getName().length()-4);
else name = file.getName();
in = AudioSystem.getAudioInputStream(file);
din = null;
AudioFormat baseFormat = in.getFormat();
decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
pause_flag = true;
//スレッド側で再生する
start();
} catch (Exception e) {
// Handle exception.
e.printStackTrace();
}
}
public boolean mp3ok(){
if(this.decodedFormat==null ||this.din==null){
return false;
}
return true;
}
public boolean mp3play(){
if(this.decodedFormat==null || this.din==null){
return false;
}
pause_flag = false;
return true;
}
public void mp3stop(){
end_flag = true;
}
public void setVolume(int vol){
if(vol <= 256 && line != null){
try{
FloatControl control;
control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue((float)Math.log10(vol/256.0F) * 20);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
public void setTime(int time){
if(time == 0){
pause_flag = true;
}
}
public boolean isPlaying(){
return playing_flag;
}
//受け取ったメッセージを実行するスレッド
@Override
public void run() {
this.setName("MP3 play");
while(!end_flag){
if(pause_flag){
while(pause_flag && !end_flag){
try { Thread.sleep(500); } catch (InterruptedException e) { }
}
continue;
}
playing_flag = true;
try {
rawplay();
} catch (IOException e) {
e.printStackTrace();
}
playing_flag = false;
pause_flag = true;
try {
// Stop
din.close();
in.close();
in = AudioSystem.getAudioInputStream(file);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
//停止
try {
line.close();
line = null;
din.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void rawplay(/*AudioFormat targetFormat, AudioInputStream din*/)
throws IOException {
byte[] data = new byte[8192];
try {
line = getLine(decodedFormat);
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
if (line != null) {
// Start
line.start();
int nBytesRead = 0/*, nBytesWritten = 0*/;
while (nBytesRead != -1 && !end_flag && !pause_flag) {
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1)
/*nBytesWritten =*/ line.write(data, 0, nBytesRead);
}
line.drain();//再生が終わるまで待つ
line.stop();
}
}
private SourceDataLine getLine(AudioFormat audioFormat)
throws LineUnavailableException {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat, 524288);//512KBくらいあればOK?
return res;
}
}