Adding Cover Art to downloaded mp3 on Android

て烟熏妆下的殇ゞ 提交于 2019-12-03 06:26:15

问题


How do I add a cover art (album art) to a downloaded mp3 programmatically?

I am letting my app download a mp3 file via the DownloadManager from the Android APIS as shown below:

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);
}

Is it possible to add a coverart to the downloaded mp3 file, so that the mp3 player on the phone will show a image while playing the song?


回答1:


Download this jar file MyID3_for_android and add it to your project's build path. here is a sample code of how you can change the cover

        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); 

You Will need also the Jakarta Regex jar

readFile is only a function to get byte[] from File

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();
    }
}

Got it from stackOverflow , dont know from who :)



来源:https://stackoverflow.com/questions/11510075/adding-cover-art-to-downloaded-mp3-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!