📄 mp3file.java
字号:
/** * Copyright (C) 2001 Jonathan Hilliker * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Description: * This class is a container of all the tags and data that can be extracted * from the mp3 specified by the file. If there are no id3 tags present, * tags will be created by using mutators and saving the data. * * @author: Jonathan Hilliker * @version: $Id: MP3File.java,v 1.3 2001/10/28 06:08:49 helliker Exp $ * Revsisions: * $Log: MP3File.java,v $ * Revision 1.3 2001/10/28 06:08:49 helliker * Writes id3v2 tags before id3v1 tags. This fixes a bug where if the filesize is * changed when the id3v2 tag is written out then the id3v1 tag will not be at * the end of the file and won't be recognized. * * Revision 1.2 2001/10/19 03:57:53 helliker * All set for release. * * */package helliker.id3;import java.io.*;public class MP3File implements Comparable { /** * Write ID3v1 and ID3v2 tags whether or not they exist. Precedence for * reading will be given to id3v2 tags. */ public static final int BOTH_TAGS = 0; /** * Write and read from ID3v2 tags only. An ID3v2 tag will be created * if an attempt is made to write. */ public static final int ID3V2_ONLY = 1; /** * Write and read from ID3v1 tags only. An ID3v1 tag will be created * if an attempt is made to write. */ public static final int ID3V1_ONLY = 2; /** * Do not write or read from any id3 tags. */ public static final int NO_TAGS = 3; /** * Only write and read tags that already exist. Existing tags can be * updated but new tags will not be created. */ public static final int EXISTING_TAGS_ONLY = 4; private final int ID3V1 = 5; private final int ID3V2 = 6; private ID3v1Tag id3v1 = null; private ID3v2Tag id3v2 = null; private MPEGAudioFrameHeader head = null; private MP3Comparator comparator = null; private File mp3 = null; private int tagType = 0; /** * Create an MP3File object that reads and writes to the file with the * filename fn. This assumes that you only want to read * and write id3 tags that already exist in the file. * * @param fn the filename of the mp3 * @exception FileNotFoundException if an error occurs * @exception NoMPEGFramesException if an error occurs * @exception IOException if an error occurs * @exception ID3v2FormatException if an error occurs */ public MP3File( String fn ) throws FileNotFoundException, NoMPEGFramesException, IOException, ID3v2FormatException { this( new File( fn ) ); } /** * Create an MP3File object that reads and writes to the specified * file. This assumes that you only want to read and write * id3 tags tha already exist in the file. * * @param mp3 the file of the mp3 * @exception FileNotFoundException if an error occurs * @exception NoMPEGFramesException if an error occurs * @exception IOException if an error occurs * @exception ID3v2FormatException if an error occurs */ public MP3File( File mp3 ) throws FileNotFoundException, NoMPEGFramesException, IOException, ID3v2FormatException { this( mp3, EXISTING_TAGS_ONLY ); } /** * Create an MP3File object that reads and writes to the file with the * filename fn. The id3 tags that are read from and written are * dependant upon the tagType argument. This could be either: BOTH_TAGS, * ID3V2_ONLY, ID3V1_ONLY, NO_TAGS, or EXISTING_TAGS_ONLY. * * @param fn the filename of the mp3 * @param tagType determines what type of tags to write and read from * @exception FileNotFoundException if an error occurs * @exception NoMPEGFramesException if an error occurs * @exception IOException if an error occurs * @exception ID3v2FormatException if an error occurs */ public MP3File( String fn, int tagType ) throws FileNotFoundException, NoMPEGFramesException, IOException, ID3v2FormatException { this( new File( fn ), tagType ); } /** * Create and MP3File object that reads and writes to the specified file. * The id3 tags that are read from and written are * dependant upon the tagType argument. This could be either: BOTH_TAGS, * ID3V2_ONLY, ID3V1_ONLY, NO_TAGS, or EXISTING_TAGS_ONLY. * * @param mp3 the file of the mp3 * @param tagType determines what type of tags to write and read from * @exception FileNotFoundException if an error occurs * @exception NoMPEGFramesException if an error occurs * @exception IOException if an error occurs * @exception ID3v2FormatException if an error occurs */ public MP3File( File mp3, int tagType ) throws FileNotFoundException, NoMPEGFramesException, IOException, ID3v2FormatException { this.mp3 = mp3; this.tagType = tagType; id3v1 = new ID3v1Tag( mp3 ); id3v2 = new ID3v2Tag( mp3 ); head = new MPEGAudioFrameHeader( mp3, id3v2.getSize() ); comparator = new MP3Comparator(); } /** * Returns the length (in seconds) of the playing time of this mp3. This * will not return an accurate value for VBR files. * * @return the playing time (in seconds) of this mp3 */ public long getPlayingTime() { long datasize = (mp3.length() * 8) - id3v2.getSize(); long bps = head.getBitRate() * 1000; return datasize / bps; } /** * Return a formatted version of the getPlayingTime method. The string * will be formated "m:ss" where 'm' is minutes and 'ss' is seconds. * * @return a formatted version of the getPlayingTime method */ public String getPlayingTimeString() { String str; long time = getPlayingTime(); long mins = time / 60; long secs = Math.round((((double)time / 60) - (long)(time / 60)) * 60); str = mins + ":"; if( secs < 10 ) { str += "0" + secs; } else { str += "" + secs; } return str; } /** * Return the absolute path of this MP3File. * * @return the absolute path of this MP3File */ public String getPath() { return mp3.getAbsolutePath(); } /** * Returns the parent directory of this MP3File. * * @return the parent directory of this MP3File */ public String getParent() { return mp3.getParent(); } /** * Returns the filename of this MP3File. * * @return the filename of this MP3File */ public String getFileName() { return mp3.getName(); } /** * Return the filesize of this MP3File (in bytes). * * @return the filesize of this MP3File (in bytes) */ public long getFileSize() { return mp3.length(); } /** * Returns true if an id3v2 tag currently exists. * * @return true if an id3v2 tag currently exists */ public boolean id3v2Exists() { return id3v2.tagExists(); } /** * Returns true if an id3v1 tag currently exists. * * @return true if an id3v1 tag currently exists */ public boolean id3v1Exists() { return id3v1.tagExists(); } /** * Returns true if this file is an mp3. This means simply that an * MPEGAudioFrameHeader was found and the layer is 3. * * @return true if this file is an mp3 */ public boolean isMP3() { return head.isMP3(); } /** * Returns the bit rate of this mp3 in kbps. * * @return the bit rate of this mp3 in kbps */ public int getBitRate() { return head.getBitRate(); } /** * Returns the sample rate of this mp3 in Hz. * * @return the sample reate of this mp3 in Hz */ public int getSampleRate() { return head.getSampleRate(); } /** * Returns the emphasis of this mp3. * * @return the emphasis of this mp3 */ public String getMPEGEmphasis() { return head.getEmphasis(); } /** * Returns a string specifying the layer of the mpeg. Ex: Layer III * * @return a string specifying the layer of the mpeg */ public String getMPEGLayer() { return head.getLayer(); } /** * Returns a string specifying the version of the mpeg. This can either * be 1.0, 2.0, or 2.5. * * @return a string specifying the version of the mpeg */ public String getMPEGVersion() { return head.getVersion(); } /** * Return the channel mode of the mpeg. Ex: Stereo * * @return the channel mode of the mpeg */ public String getMPEGChannelMode() { return head.getChannelMode(); } /** * Returns true if this mpeg is copyrighted. * * @return true if this mpeg is copyrighted */ public boolean isMPEGCopyrighted() { return head.isCopyrighted(); } /** * Returns true if this mpeg is the original. * * @return true if this mpeg is the original */ public boolean isMPEGOriginal() { return head.isOriginal(); } /** * Returns true if this mpeg is protected by CRC. * * @return true if this mpeg is protected by CRC */ public boolean isMPEGProtected() { return head.isProtected(); } /** * Removes id3 tags from the file. The argument specifies which tags to * remove. This can either be BOTH_TAGS, ID3V1_ONLY, ID3V2_ONLY, or * EXISTING_TAGS_ONLY. * * @param type specifies what tag(s) to remove * @exception FileNotFoundException if an error occurs * @exception IOException if an error occurs */ public void removeTags( int type ) throws FileNotFoundException, IOException { if( allow( ID3V1, type ) ) { id3v1.removeTag(); } if( allow( ID3V2, type ) ) { id3v2.removeTag(); } } /** * Writes the current state of the id3 tags to the file. What tags are * written depends upon the tagType passed to the constructor. * * @exception FileNotFoundException if an error occurs * @exception IOException if an error occurs */ public void writeTags() throws FileNotFoundException, IOException { // Write out id3v2 first because if the filesize is changed when an // id3v2 is written then the id3v1 may be moved away from the end // of the file which would cause it to not be recognized. if( allow( ID3V2 ) ) { id3v2.writeTag(); } if( allow( ID3V1 ) ) { id3v1.writeTag(); } } /** * Set the title of this mp3. * * @param title the title of the mp3 */ public void setTitle( String title ) { if( allow( ID3V1 ) ) { id3v1.setTitle( title ); } if( allow( ID3V2 ) ) { id3v2.setTextFrame( ID3v2Frames.TITLE, title ); } } /** * Set the album of this mp3. * * @param album the album of the mp3 */ public void setAlbum( String album ) { if( allow( ID3V1 ) ) { id3v1.setAlbum( album ); } if( allow( ID3V2 ) ) { id3v2.setTextFrame( ID3v2Frames.ALBUM, album ); } } /** * Set the artist of this mp3. * * @param artist the artist of the mp3 */ public void setArtist( String artist ) { if( allow( ID3V1 ) ) { id3v1.setArtist( artist ); } if( allow( ID3V2 ) ) { id3v2.setTextFrame( ID3v2Frames.LEAD_PERFORMERS, artist ); } } /** * Add a comment to this mp3. * * @param comment a comment to add to the mp3 */ public void setComment( String comment ) { if( allow( ID3V1 ) ) { id3v1.setComment( comment ); } if( allow( ID3V2 ) ) { id3v2.setCommentFrame( "", comment ); } } /** * Set the genre of this mp3. * * @param genre the genre of the mp3 */ public void setGenre( String genre ) { if( allow( ID3V1 ) ) { id3v1.setGenreString( genre ); } if( allow( ID3V2 ) ) { id3v2.setTextFrame( ID3v2Frames.CONTENT_TYPE, genre ); } } /** * Set the year of this mp3. * * @param year of the mp3 */ public void setYear( String year ) { if( allow( ID3V1 ) ) { id3v1.setYear( year ); } if( allow( ID3V2 ) ) { id3v2.setTextFrame( ID3v2Frames.YEAR, year ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -