1

如何以编程方式将封面艺术(专辑艺术)添加到下载的 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 文件中添加封面,以便手机上的 mp3 播放器在播放歌曲时显示图像?

4

1 回答 1

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 得到它,不知道从谁:)

于 2012-07-19T14:53:49.913 回答