在Android上为已下载的mp3添加封面艺术

时间:2012-07-16 18:14:14

标签: android mp3 id3 download-manager id3-tag

如何以编程方式将封面图片(专辑封面)添加到下载的mp3中?

我让我的应用程序从Android APIS通过DownloadManager下载mp3文件,如下所示:

private void startDownload(){
    Uri uri = Uri.parse(BASE_URL+DOWNLOAD+mp3+".mp3");

    filename = title+".mp3";

    Request request = new DownloadManager.Request(uri)
    .setDestinationInExternalPublicDir(MUSIC_PATH, filename);

    DOWNLOAD_MANAGER.enqueue(request);
}

是否可以在下载的mp3文件中添加一个coverart,以便手机上的mp3播放器在播放歌曲时会显示图像?

1 个答案:

答案 0 :(得分:9)

下载此jar文件MyID3_for_android并将其添加到项目的构建路径中。这是一个如何更改封面的示例代码

        File src = new File("filepath");
        MusicMetadataSet src_set = new MyID3().read(src);

        File img = (new File("imagePath"));


        Vector<ImageData> fileList = new Vector<ImageData>();
        ImageData data = new ImageData(readFile(img), "", "", 3);
        fileList.add(data);

        MusicMetadata metadata =  (MusicMetadata) src_set.getSimplified();
        metadata.setPictureList(fileList);

        File dst = new File("destinationPath");

        new MyID3().write(src, dst, src_set, metadata); 

您还需要Jakarta Regex jar

readFile只是从File

获取byte []的函数
public static byte[] readFile (File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");

    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength) throw new IOException("File size >= 2 GB");

        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    }
    finally {
        f.close();
    }
}

从stackOverflow得到它,不知道是谁:)