I am trying to find ID3V2 tags from MP3 file using jid3lib in Java. But I am getting messy strings. Here is my code:

import org.farng.mp3.MP3File;
import org.farng.mp3.id3.AbstractID3v2;

public class NewClass {
    public static void main(String [] args){
        try{
            MP3File file = new MP3File("c:\\2.mp3");
            AbstractID3v2 tag = file.getID3v2Tag();
            String str = tag.getAuthorComposer();
            System.out.println("Lenght: "+str.length());
            for(int i = 0; i < str.length(); i++){
                char ch = str.charAt(i);
                System.out.println(i+": "+"'"+(ch)+"' : "+((int)ch));
            }
            System.out.println(str);

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

And I am getting following output:

Lenght: 35
0: '�' : 65533
1: '�' : 65533
2: 'H' : 72
3: ' ' : 0
4: 'i' : 105
5: ' ' : 0
6: 'm' : 109
7: ' ' : 0
8: 'e' : 101
9: ' ' : 0
10: 's' : 115
11: ' ' : 0
12: 'h' : 104
13: ' ' : 0
14: ' ' : 32
15: ' ' : 0
16: 'R' : 82
17: ' ' : 0
18: 'e' : 101
19: ' ' : 0
20: 's' : 115
21: ' ' : 0
22: 'h' : 104
23: ' ' : 0
24: 'a' : 97
25: ' ' : 0
26: 'm' : 109
27: ' ' : 0
28: 'm' : 109
29: ' ' : 0
30: 'i' : 105
31: ' ' : 0
32: 'y' : 121
33: ' ' : 0
34: 'a' : 97
��H i m e s h   R e s h a m m i y a

But in Windows Explorer it shows correct "Himesh Reshammiya".

How can I get this correct String in java, or any solution to decode this string?

I think this may be some character encoding. Thanks in advance

有帮助吗?

解决方案

I'll hazard a guess that the library doesn't support UTF-16 encoded text, which that appears to be (The first two characters listed in the output are the BOM).

You should probably use one of the libraries listed here: http://id3.org/Implementations

Looking through the Java ones, this could be a good one: https://github.com/mpatric/mp3agic

It says it supports Unicode right on the page.

其他提示

Possible workaround:

replaceAll("\\s","")

This removes all whitespaces and non-visible characters.

3: ' ' : 0 your problem is this as i see?

just put this ' ' char (which is no space) in a new Integer() and it must be equals 0.

use mp3agic.. its awesome..... http://mpatric.github.io/mp3agic/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top