如何在Android的mp3文件中添加封面

时间:2019-01-15 10:40:50

标签: android mp3 id3

如何在Android中更改或向mp3文件添加封面?

我使用了这个answer,但是无法播放输出。这是我的代码:

  File f = new File(Environment.getExternalStorageDirectory(), "stage");
    if (!f.exists()) {
        f.mkdirs();
    }
    File src = new File(Environment.getExternalStorageDirectory() + "/" + "stage", "music" + ".mp3");

    MusicMetadataSet src_set = null;
    try {
        src_set = new MyID3().read(src);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String completePath1 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+ "img.jpg";
    File img = new File(completePath1);

    Vector<ImageData> fileList = new Vector<ImageData>();
    ImageData data = null;
    try {
        data = new ImageData(readFile(img), "", "", 3);
    } catch (IOException e) {
        e.printStackTrace();
    }
    fileList.add(data);

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

    File dst = new File(Environment.getExternalStorageDirectory() + "/" + "stage", "output" + ".mp3");

    try {
        new MyID3().write(src, dst, src_set, metadata);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ID3WriteException e) {
        e.printStackTrace();
    }

和readFile方法:

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

我在代码中使用MyID3_for_androidJakarta Regex jar库。还有其他方法可以完成这项工作吗?

0 个答案:

没有答案