1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.io;
21
22 import org.xnap.XNap;
23 import org.xnap.util.Formatter;
24
25 /***
26 * MP3 header format documented at:
27 * http://mp3tech.free.fr/programmers/frame_header.html
28 */
29 public class MP3MetaInfo {
30
31
32
33
34
35 private MetaInfoFile file;
36
37
38
39 public MP3MetaInfo(MetaInfoFile file)
40 {
41 this.file = file;
42 }
43
44
45
46 public int getBitrate()
47 {
48 return ((Integer)file.get(MetaInfoFile.BITRATE)).intValue();
49 }
50
51 public int getFrequency()
52 {
53 return ((Integer)file.get(MetaInfoFile.FREQUENCY)).intValue();
54 }
55
56 /***
57 * Returns the length of the file in seconds.
58 */
59 public long getPlayingTime()
60 {
61 return ((Long)file.get(MetaInfoFile.LENGTH)).longValue();
62 }
63
64 public String getInfo()
65 {
66 return XNap.tr("{0} bit, {1}", new Integer(getBitrate()),
67 Formatter.formatLength(getPlayingTime()));
68 }
69
70 public static boolean hasInfo(MetaInfoFile file)
71 {
72 return "true".equals(file.get("provider:mp3"));
73 }
74
75 /***
76 * @see org.xnap.io.MetaInfoProvider#handle(org.xnap.io.MetaInfoFile)
77 */
78 public static boolean handle(MetaInfoFile file)
79 {
80 try {
81 helliker.id3.MP3File m = new helliker.id3.MP3File(file);
82
83 file.put(MetaInfoFile.BITRATE, new Integer(m.getBitRate()));
84 file.put(MetaInfoFile.FREQUENCY, new Integer(m.getSampleRate()));
85 file.put(MetaInfoFile.LENGTH, new Long(m.getPlayingTime()));
86 file.put("provider:mp3", "true");
87 return true;
88 }
89 catch (Exception e) {
90 return false;
91 }
92 }
93
94 public static class Provider implements MetaInfoProvider {
95
96 /***
97 * @see org.xnap.io.MetaInfoProvider#handle(org.xnap.io.MetaInfoFile)
98 */
99 public boolean handle(MetaInfoFile file)
100 {
101 return MP3MetaInfo.handle(file);
102 }
103
104 }
105
106 }