-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSong.java
More file actions
160 lines (144 loc) · 4.12 KB
/
Song.java
File metadata and controls
160 lines (144 loc) · 4.12 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
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
import org.tritonus.share.sampled.file.TAudioFileFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;
import java.io.*;
/**
* The Responder class keeping the mp3 file
*
* @author omid mahyar and pouyan hesabi *
* @version 1.0 (1398/04/01)
*/
public class Song implements Serializable {
private String path;
private String name;
private String AlbumName;
private String artistName;
private transient Mp3File mp3file;
private int timeOfSong;
private transient ID3v2 id3v2Tag;
private byte[] imageData;
private String picType;
public Song(String path, String name) {
this.name = name;
this.path = path;
creatMp3File();
setArtistName();
songPic();
setAlbumName();
setSongTime();
}
/**
* extracting mp3 file info
*/
public void creatMp3File() {
{
try {
mp3file = new Mp3File(path);
id3v2Tag = mp3file.getId3v2Tag();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedTagException e) {
e.printStackTrace();
} catch (InvalidDataException e) {
e.printStackTrace();
}
}
}
/**
* extracting mp3 file time
*/
public void setSongTime() {
File file = new File(path);
Long microseconds;
AudioFileFormat fileFormat = null;
try {
fileFormat = AudioSystem.getAudioFileFormat(file);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (fileFormat instanceof TAudioFileFormat) {
Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
String key = "duration";
microseconds = (Long) properties.get(key);
timeOfSong = (int) (microseconds / 1000000);
} else {
try {
throw new UnsupportedAudioFileException();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
}
/**
* extracting mp3 file picture
*/
public void songPic() {
if (mp3file.hasId3v2Tag()) {
imageData = id3v2Tag.getAlbumImage();
if (imageData != null) {
picType = id3v2Tag.getAlbumImageMimeType().substring(6);
} else {
File fi = new File("icons/1.jpg");
try {
imageData = Files.readAllBytes(fi.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* seting mp3 file picture
*/
public void setArtistName() {
if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
artistName = id3v2Tag.getAlbumArtist();
if (artistName == null)
artistName = "NO ARTIST NAME";
}
}
/**
* seting mp3 file Album
*/
public void setAlbumName() {
if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
AlbumName = id3v2Tag.getAlbum();
if (AlbumName == null)
AlbumName = "NO ALBUM NAME";
}
}
public String getArtistName() {
return artistName;
}
public String getPath() {
return path;
}
public byte[] getImageData() {
return imageData;
}
public String getPicType() {
return picType;
}
public String getName() {
return name;
}
public String getAlbumName() {
return AlbumName;
}
public int getTimeOfSong() {
return timeOfSong;
}
}