Android使用jaudiotagger

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

主app.java:import java.io.File; import 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());
        }
        */




    }
}

Logcat:

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):at android.app .ActivityThread.performLaunchActivity(ActivityThread.java:1651)02-22 21:12:22.546:E / AndroidRuntime(19738):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)02-22 21:12:22.546 :E / AndroidRuntime(19738):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)02-22 21:12:22.546:E / AndroidRuntime(19738):at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:935)02-22 21:12:22.546:E / AndroidRuntime(19738):at android.os.Handler.dispatchMessage(Handler.java:99)02-22 21:12:22.546:E / AndroidRuntime (19738):at android.os.Looper.loop(Looper.java:130)02-22 21:12:22.546:E / AndroidRuntime(19738):at andro id.app.ActivityThread.main(ActivityThread.java:3691)02-22 21:12:22.546:E / AndroidRuntime(19738):at java.lang.reflect.Method.invokeNative(Native Method)02-22 21:12 :22.546:E / AndroidRuntime(19738):在java.lang.reflect.Method.invoke(Method.java:507)02-22 21:12:22.546:E / AndroidRuntime(19738):at com.android.internal。 os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:907)02-22 21:12:22.546:E / AndroidRuntime(19738):at 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):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)02-22 21:12:22.546:E / AndroidRuntime(19738):at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1615)02-22 21:12:22.546:E / AndroidRuntime(19738):... 11更多


看起来像这条线导致崩溃,因为f是空的:

Tag tag = f.getTag();

你不应该以你的方式忽略异常,就像你有一个异常,你只是打印一些东西并继续一个坏的状态(在这种情况下 - f仍然是空的)


在Android上使用jaudiotagger有两个拦截器:

1 - javax.swing

2 - javax.imageio

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

要解决您的问题:修复源代码,以便它们不再依赖于这两个JAVAX类


在获取标签之前,您仍然需要检查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;
链接地址: http://www.djcxy.com/p/96805.html

上一篇: Android using jaudiotagger

下一篇: Lame MP3 Encoder compile for Android