3

我尝试像这样使用 jaudiotagger 但它崩溃了

主app.java:导入java.io.File;导入 java.io.IOException;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.FieldDataInvalidException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.KeyNotFoundException;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class App extends Activity {
    /** Called when the activity is first created. */


    private TextView txt1;
    private TextView txt2;
    private TextView txt3;
    private TextView txt4;
    private TextView txt5;
    private TextView txt6;
    private TextView txt7;
    private TextView txt8;
    private TextView txt9;






    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       // try
        //{
        File mp3 = new File("/sdcard/test.mp3");
        AudioFile f = null;
        try {
            f = AudioFileIO.read(mp3);
        } catch (CannotReadException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (TagException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (ReadOnlyFileException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (InvalidAudioFrameException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        }
        Tag tag = f.getTag();
        AudioHeader AudioHeader = f.getAudioHeader();
        txt1.setText(tag.getFirst(FieldKey.ARTIST));
        txt2.setText(tag.getFirst(FieldKey.ALBUM));
        txt3.setText(tag.getFirst(FieldKey.TITLE));
        txt4.setText(tag.getFirst(FieldKey.COMMENT));
        txt5.setText(tag.getFirst(FieldKey.YEAR));
        txt6.setText(tag.getFirst(FieldKey.TRACK));
        txt7.setText(tag.getFirst(FieldKey.DISC_NO));
        txt8.setText(tag.getFirst(FieldKey.COMPOSER));
        txt9.setText(tag.getFirst(FieldKey.ARTIST_SORT));




        try {
            tag.setField(FieldKey.ARTIST,"Kings of Leon");
        } catch (KeyNotFoundException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (FieldDataInvalidException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        }
        try {
            AudioFileIO.write(f);
        } catch (CannotWriteException e) {
            txt1.setText(e.toString());
        }
       /* }
        catch(Exception x)
        {
            txt1.setText(x.toString());
        }
        */




    }
}

日志猫:

02-22 21:12:22.546: E/AndroidRuntime(19738): 致命异常: main 02-22 21:12:22.546: E/AndroidRuntime(19738): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. mp3.tag.editor.alexander.fuchs/com.mp3.tag.editor.alexander.fuchs.App}:java.lang.NullPointerException 02-22 21:12:22.546:E/AndroidRuntime(19738):在 android.app .ActivityThread.performLaunchActivity(ActivityThread.java:1651) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 02-22 21:12:22.546 : E/AndroidRuntime(19738): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.app.ActivityThread$H.handleMessage (ActivityThread.java:935) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.os。Handler.dispatchMessage(Handler.java:99) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.os.Looper.loop(Looper.java:130) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.app.ActivityThread.main(ActivityThread.java:3691) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 java.lang.reflect.Method.invokeNative(Native方法) 02-22 21:12:22.546: E/AndroidRuntime(19738): at java.lang.reflect.Method.invoke(Method.java:507) 02-22 21:12:22.546: E/AndroidRuntime(19738) : 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:665) 02-22 21:12:22.546: E/AndroidRuntime(19738): at dalvik.system.NativeStart.main(Native Method) 02-22 21:12:22.546: E/AndroidRuntime(19738) :引起:java.lang.NullPointerException 02-22 21:12:22.546: E/AndroidRuntime(19738): at com.mp3.tag.editor.alexander.fuchs.App.onCreate(App.java:72) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-22 21:12:22.546: E/AndroidRuntime(19738): 在 android.app.ActivityThread .performLaunchActivity(ActivityThread.java:1615) 02-22 21:12:22.546: E/AndroidRuntime(19738): ... 11 更多performLaunchActivity(ActivityThread.java:1615) 02-22 21:12:22.546: E/AndroidRuntime(19738): ... 11 更多performLaunchActivity(ActivityThread.java:1615) 02-22 21:12:22.546: E/AndroidRuntime(19738): ... 11 更多

4

3 回答 3

2

在 Android 上使用 jaudiotagger 有两个障碍:

1 - javax.swing

2 - javax.imageio

android 不支持这两个类,jaudiotagger 使用它们

解决您的问题:修复源代码,使其不再依赖这两个 JAVAX 类

于 2013-06-23T13:06:09.403 回答
1

似乎这一行会导致崩溃,因为f它是空的:

Tag tag = f.getTag();

您不应该以您的方式忽略异常,就好像您遇到异常一样,您只需打印一些内容并以错误状态继续(在这种情况下 -f仍然为空)

于 2012-02-22T20:20:10.760 回答
0

在获取标签之前,您仍然需要检查 f != null 。

Tag r_tag = null;
File testFile = new File(filename);
MP3File f = null;
try {
    f = (MP3File)AudioFileIO.read(testFile);
} catch (CannotReadException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (TagException e) {
    e.printStackTrace();
} catch (ReadOnlyFileException e) {
    e.printStackTrace();
} catch (InvalidAudioFrameException e) {
    e.printStackTrace();
}
if(f != null) {
    f.getTagOrCreateAndSetDefault();
    r_tag = f.getID3v2TagAsv24();
}
return r_tag;
于 2013-03-02T20:46:29.920 回答