⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3file.java

📁 java+eclipse做的TTPlayer
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            fc.read(bb);

            //Write bytes to outputFile
            FileOutputStream out = new FileOutputStream(outputFile);
            out.write(bb.array());
            out.close();
            fc.close();
            fis.close();
            return outputFile;
        }
        throw new TagNotFoundException("There is no ID3v2Tag data in this file");
    }

    /**
     * Return audio header
     */
    public MP3AudioHeader getMP3AudioHeader() {
        return (MP3AudioHeader) getAudioHeader();
    }

    /**
     * Returns true if this datatype contains an <code>Id3v1</code> tag
     *
     * @return true if this datatype contains an <code>Id3v1</code> tag
     */
    public boolean hasID3v1Tag() {
        return (id3v1tag != null);
    }

    public boolean hasAPEv2Tag() {
        return apev2Tag != null;
    }

    /**
     * Returns true if this datatype contains an <code>Id3v2</code> tag
     *
     * @return true if this datatype contains an <code>Id3v2</code> tag
     */
    public boolean hasID3v2Tag() {
        return (id3v2tag != null);
    }

    /**
     * Returns true if this datatype contains a <code>Lyrics3</code> tag
     * TODO disabled until Lyrics3 fixed
     * @return true if this datatype contains a <code>Lyrics3</code> tag
     */
    /*
    public boolean hasLyrics3Tag()
    {
    return (lyrics3tag != null);
    }
     */
    /**
     * Creates a new MP3File datatype and parse the tag from the given file
     * Object.
     *
     * @param file MP3 file
     * @throws IOException  on any I/O error
     * @throws TagException on any exception generated by this library.
     */
    public MP3File(File file) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
        this(file, LOAD_ALL);
    }

    /**
     * Sets the ID3v1(_1)tag to the tag provided as an argument.
     *
     * @param id3v1tag
     */
    public void setID3v1Tag(ID3v1Tag id3v1tag) {
        log.info("setting tagv1:v1 tag");
        this.id3v1tag = id3v1tag;
    }

    public void setID3v1Tag(Tag id3v1tag) {
        log.info("setting tagv1:v1 tag");
        this.id3v1tag = (ID3v1Tag) id3v1tag;
    }

    public void setAPEv2Tag(APEv2Tag tag) {
        log.log(Level.INFO, "设置了apev2的tag");
        this.apev2Tag = tag;
    }

    /**
     * Sets the <code>ID3v1</code> tag for this datatype. A new
     * <code>ID3v1_1</code> datatype is created from the argument and then used
     * here.
     *
     * @param mp3tag Any MP3Tag datatype can be used and will be converted into a
     *               new ID3v1_1 datatype.
     */
    public void setID3v1Tag(AbstractTag mp3tag) {
        log.info("setting tagv1:abstract");
        id3v1tag = new ID3v11Tag(mp3tag);
    }

    /**
     * Returns the <code>ID3v1</code> tag for this datatype.
     *
     * @return the <code>ID3v1</code> tag for this datatype
     */
    public ID3v1Tag getID3v1Tag() {
        return id3v1tag;
    }

    public APEv2Tag getAPEv2Tag() {
        return apev2Tag;
    }

    /**
     * Sets the <code>ID3v2</code> tag for this datatype. A new
     * <code>ID3v2_4</code> datatype is created from the argument and then used
     * here.
     *
     * @param mp3tag Any MP3Tag datatype can be used and will be converted into a
     *               new ID3v2_4 datatype.
     */
    public void setID3v2Tag(AbstractTag mp3tag) {
        id3v2tag = new ID3v24Tag(mp3tag);

    }

    /**
     * Sets the v2 tag to the v2 tag provided as an argument.
     * Also store a v24 version of tag as v24 is the interface to be used
     * when talking with client applications.
     *
     * @param id3v2tag
     */
    public void setID3v2Tag(AbstractID3v2Tag id3v2tag) {
        this.id3v2tag = id3v2tag;
        if (id3v2tag instanceof ID3v24Tag) {
            this.id3v2Asv24tag = (ID3v24Tag) this.id3v2tag;
        } else {
            this.id3v2Asv24tag = new ID3v24Tag(id3v2tag);
        }
    }

    /**
     * Set v2 tag ,dont need to set v24 tag because saving
     * <p/>
     * TODO temp its rather messy
     */
    public void setID3v2TagOnly(AbstractID3v2Tag id3v2tag) {
        this.id3v2tag = id3v2tag;
        this.id3v2Asv24tag = null;
    }

    /**
     * Returns the <code>ID3v2</code> tag for this datatype.
     *
     * @return the <code>ID3v2</code> tag for this datatype
     */
    public AbstractID3v2Tag getID3v2Tag() {
        return id3v2tag;
    }

    /**
     *
     * @return a representation of tag as v24
     */
    public ID3v24Tag getID3v2TagAsv24() {
        return id3v2Asv24tag;
    }

    /**
     * Sets the <code>Lyrics3</code> tag for this datatype. A new
     * <code>Lyrics3v2</code> datatype is created from the argument and then
     * used here.
     *
     * @param mp3tag Any MP3Tag datatype can be used and will be converted into a
     *               new Lyrics3v2 datatype.
     */
    /*
    public void setLyrics3Tag(AbstractTag mp3tag)
    {
    lyrics3tag = new Lyrics3v2(mp3tag);
    }
     */
    /**
     *
     *
     * @param lyrics3tag
     */
    /*
    public void setLyrics3Tag(AbstractLyrics3 lyrics3tag)
    {
    this.lyrics3tag = lyrics3tag;
    }
     */
    /**
     * Returns the <code>ID3v1</code> tag for this datatype.
     *
     * @return the <code>ID3v1</code> tag for this datatype
     */
    /*
    public AbstractLyrics3 getLyrics3Tag()
    {
    return lyrics3tag;
    }
     */
    /**
     * Remove tag from file
     *
     * @param mp3tag
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void delete(AbstractTag mp3tag) throws FileNotFoundException, IOException {
        mp3tag.delete(new RandomAccessFile(this.file, "rw"));
    }

    /**
     * Saves the tags in this datatype to the file referred to by this datatype.
     *
     * @throws IOException  on any I/O error
     * @throws TagException on any exception generated by this library.
     */
    public void save() throws IOException, TagException {
        save(this.file);
    }

    /**
     * Overriden for comptability with merged code
     *
     * @throws CannotWriteException
     */
    public void commit() throws CannotWriteException {
        try {
            save();
        } catch (IOException ioe) {
            throw new CannotWriteException(ioe);
        } catch (TagException te) {
            throw new CannotWriteException(te);
        }
    }

    /**
     * Saves the tags in this datatype to the file argument. It will be saved as
     * TagConstants.MP3_FILE_SAVE_WRITE
     *
     * @param file file to save the this datatype's tags to
     * @throws FileNotFoundException if unable to find file
     * @throws IOException           on any I/O error
     */
    public void save(File file) throws FileNotFoundException, IOException {
        log.info("Saving  : " + file.getAbsolutePath());
        RandomAccessFile rfile = null;
        try {
            //ID3v2 Tag
            if (TagOptionSingleton.getInstance().isId3v2Save()) {
                if (id3v2tag == null) {
                    rfile = new RandomAccessFile(file, "rw");
                    //这里固定用ID3v23格式来删除,所以写入的时候也必须同时写入
                    new ID3v23Tag().delete(rfile);
                    rfile.close();
                } else {
                    id3v2tag.write(file, ((MP3AudioHeader) this.getAudioHeader()).getMp3StartByte());
                }
            }
            rfile = new RandomAccessFile(file, "rw");

            //Lyrics 3 Tag
            if (TagOptionSingleton.getInstance().isLyrics3Save()) {
                if (lyrics3tag != null) {
                    lyrics3tag.write(rfile);
                }
            }
            //ID3v1 tag
            if (TagOptionSingleton.getInstance().isId3v1Save()) {
                log.info("saving v1");
                if (id3v1tag == null) {
                    log.info("deleting v1");
                    (new ID3v1Tag()).delete(rfile);
                } else {
                    log.info("saving v1 still");
                    id3v1tag.write(rfile);
                }
            }
            if (apev2Tag == null) {
                new APEv2Tag().delete(rfile,id3v1tag!=null);
            } else {
                apev2Tag.write(rfile,id3v1tag!=null);
            }
        } catch (FileNotFoundException ex) {
            log.log(Level.SEVERE, file.getAbsolutePath() + ":Problem writing tags to file,FileNotFoundException", ex.getMessage());
            throw ex;
        } catch (IOException iex) {
            log.log(Level.SEVERE, file.getAbsolutePath() + ":Problem writing tags to file,IOException", iex.getMessage());
            throw iex;
        } catch (RuntimeException re) {
            log.log(Level.SEVERE, file.getAbsolutePath() + ":Problem writing tags to file,RuntimeException", re.getMessage());
            throw re;
        } finally {
            if (rfile != null) {
                rfile.close();
            }
        }
    }

    /**
     * Displays MP3File Structure
     */
    public String displayStructureAsXML() {
        createXMLStructureFormatter();
        this.tagFormatter.openHeadingElement("file", this.getFile().getAbsolutePath());
        if (this.getID3v1Tag() != null) {
            this.getID3v1Tag().createStructure();
        }
        if (this.getID3v2Tag() != null) {
            this.getID3v2Tag().createStructure();
        }
        this.tagFormatter.closeHeadingElement("file");
        return tagFormatter.toString();
    }

    /**
     * Displays MP3File Structure
     */
    public String displayStructureAsPlainText() {
        createPlainTextStructureFormatter();
        this.tagFormatter.openHeadingElement("file", this.getFile().getAbsolutePath());
        if (this.getID3v1Tag() != null) {
            this.getID3v1Tag().createStructure();
        }
        if (this.getID3v2Tag() != null) {
            this.getID3v2Tag().createStructure();
        }
        this.tagFormatter.closeHeadingElement("file");
        return tagFormatter.toString();
    }

    private static void createXMLStructureFormatter() {
        tagFormatter = new XMLTagDisplayFormatter();
    }

    private static void createPlainTextStructureFormatter() {
        tagFormatter = new PlainTextTagDisplayFormatter();
    }

    public static AbstractTagDisplayFormatter getStructureFormatter() {
        return tagFormatter;
    }

    /**
     * Set the Tag
     *
     * If the parameter tag is a v1tag then the v1 tag is set if v2tag then the v2tag. 
     * @param tag
     */
    public void setTag(Tag tag) {
        this.tag = tag;
        if (tag instanceof ID3v1Tag) {
            setID3v1Tag((ID3v1Tag) tag);
        } else {
            setID3v2Tag((AbstractID3v2Tag) tag);
        }

    }

    public static void main(String[] args) throws Exception {
        MP3File mp = new MP3File(new File("D:\\难道爱一个人有错吗.mp3"), 0);
//        ID3v1Tag tag = mp.getID3v1Tag();
//        System.out.println(Util.convertString(tag.getFirstArtist(), "GBK"));
//        System.out.println(Util.convertString(tag.getFirstTitle()));
//       String s= mp.displayStructureAsPlainText();
//       System.out.println(s);
        ID3v23Tag v2 = new ID3v23Tag();
        v2.addAlbum(new String("V2专辑名".getBytes("GBK"), "ISO8859-1"));
        v2.setArtist(new String("V2艺术家".getBytes("GBK"), "ISO8859-1"));
        v2.setComment(new String("V2的注释".getBytes("GBK"), "ISO8859-1"));
        v2.setTitle(new String("v2有没有人告诉你".getBytes("GBK"), "ISO8859-1"));
        v2.setGenre(new String("v2风格很怪".getBytes("GBK"), "ISO8859-1"));
        v2.setYear("2008");
        v2.setTrack("HELLO");
        mp.setID3v2Tag(v2);
        ID3v1Tag v1 = new ID3v1Tag();
        v1.setAlbum(Util.convertString("V1的专辑名", "GBK", "ISO8859-1"));
        v1.setArtist(Util.convertString("V1的东来来往", "GBK", "ISO8859-1"));
        v1.setComment(Util.convertString("V2的注释在这里呢", "GBK", "ISO8859-1"));
        v1.setTitle(Util.convertString("V1忘记你是我的错", "GBK", "ISO8859-1"));
        v1.setGenre(Util.convertString("V1流行", "GBK", "ISO8859-1"));
        v1.setYear("2007");
        mp.setID3v1Tag(v1);
        mp.save();

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -