示例#1
0
  @Override
  public BufferedImage getArtwork() {
    BufferedImage image = super.getArtwork();

    if (image == null) {
      try {
        AudioFile audioFile = AudioFileIO.read(file);
        FlacTag tag = (FlacTag) audioFile.getTag();
        if (tag != null) {
          List<MetadataBlockDataPicture> images = tag.getImages();
          if (images != null && !images.isEmpty()) {
            MetadataBlockDataPicture picture = images.get(0);
            byte[] data = picture.getImageData();
            image = imageFromData(data);
          }
        }
      } catch (Throwable e) {
        LOG.error("Unable to read cover art from flac");
      }
    }

    return image;
  }
  /**
   * This will write a custom ID3 tag (TXXX). This works only with MP3 files (Flac with ID3-Tag not
   * tested).
   *
   * @param description The description of the custom tag i.e. "catalognr" There can only be one
   *     custom TXXX tag with that description in one MP3 file
   * @param text The actual text to be written into the new tag field
   * @return True if the tag has been properly written, false otherwise
   */
  public static boolean setCustomTag(AudioFile audioFile, String description, String text)
      throws IOException {
    FrameBodyTXXX txxxBody = new FrameBodyTXXX();
    txxxBody.setDescription(description);
    txxxBody.setText(text);

    // Get the tag from the audio file
    // If there is no ID3Tag create an ID3v2.3 tag
    Tag tag = audioFile.getTagOrCreateAndSetDefault();
    if (tag instanceof AbstractID3Tag) {
      // If there is only a ID3v1 tag, copy data into new ID3v2.3 tag
      if (!(tag instanceof ID3v23Tag || tag instanceof ID3v24Tag)) {
        Tag newTagV23 = null;
        if (tag instanceof ID3v1Tag) {
          newTagV23 =
              new ID3v23Tag((ID3v1Tag) audioFile.getTag()); // Copy old tag data
        }
        if (tag instanceof ID3v22Tag) {
          newTagV23 =
              new ID3v23Tag((ID3v22Tag) audioFile.getTag()); // Copy old tag data
        }
        audioFile.setTag(newTagV23);
        tag = newTagV23;
      }

      AbstractID3v2Frame frame = null;
      if (tag instanceof ID3v23Tag) {
        if (((ID3v23Tag) audioFile.getTag()).getInvalidFrames() > 0) {
          throw new IOException("read some invalid frames!");
        }
        frame = new ID3v23Frame("TXXX");
      } else if (tag instanceof ID3v24Tag) {
        if (((ID3v24Tag) audioFile.getTag()).getInvalidFrames() > 0) {
          throw new IOException("read some invalid frames!");
        }
        frame = new ID3v24Frame("TXXX");
      }

      frame.setBody(txxxBody);

      try {
        tag.setField(frame);
      } catch (FieldDataInvalidException e) {
        Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, e);
        return false;
      }
    } else if (tag instanceof FlacTag) {
      try {
        ((FlacTag) tag).setField(description, text);
      } catch (KeyNotFoundException ex) {
        Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
        return false;
      } catch (FieldDataInvalidException ex) {
        return false;
      }
    } else if (tag instanceof Mp4Tag) {
      // TagField field = new Mp4TagTextField("----:com.apple.iTunes:"+description, text);
      TagField field;
      field =
          new Mp4TagReverseDnsField(
              Mp4TagReverseDnsField.IDENTIFIER + ":" + "com.apple.iTunes" + ":" + description,
              "com.apple.iTunes",
              description,
              text);
      // TagField field = new Mp4TagTextField(description, text);
      try {
        tag.setField(field);
      } catch (FieldDataInvalidException ex) {
        Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
        return false;
      }
    } else if (tag instanceof VorbisCommentTag) {
      try {
        ((VorbisCommentTag) tag).setField(description, text);
      } catch (KeyNotFoundException ex) {
        Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
        return false;
      } catch (FieldDataInvalidException ex) {
        Logger.getLogger(TrackAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
        return false;
      }
    } else {
      // tag not implented
      Logger.getLogger(TrackAnalyzer.class.getName())
          .log(
              Level.WARNING,
              "couldn't write key information for "
                  + audioFile.getFile().getName()
                  + " to tag, because this format is not supported.");
      return false;
    }

    // write changes in tag to file
    try {
      audioFile.commit();
    } catch (CannotWriteException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }