简体   繁体   中英

How to put all mp3 files from raw folder into a list by name and other metadata?

I'm developing an app that can play MP3 files. I want to convert mp3 files inside the raw folder into an ArrayList of song objects, using the mp3 file's name as its title. Here's my code. I've taken it from this question's answer but it doesn't seem to work for me.

public void getSongList() {
    try {
        Log.d("Test", "reached");
        Field[] fields = R.raw.class.getDeclaredFields();

        Log.d("Test", "reached2");
        for (Field f : fields) {
            allSongs.addSong(new Song(0, f.getName(), ""));
        }
        Log.d("Test", "reached3");
    } catch (Exception e) {
        Log.d("Test", "reached4");
        Log.d("Test", allSongs.getSongs().size() + "");
        Log.d("Test", e.toString());
        return;
    }
}

Here are the logs:

07-03 15:50:52.497 17106-17106/poc.cevt.hmi.com.musicplayerapp D/Test: reached
07-03 15:50:52.497 17106-17106/poc.cevt.hmi.com.musicplayerapp D/Test: reached2
07-03 15:50:52.498 17106-17106/poc.cevt.hmi.com.musicplayerapp D/Test: reached4
07-03 15:50:52.498 17106-17106/poc.cevt.hmi.com.musicplayerapp D/Test: 0
07-03 15:50:52.498 17106-17106/poc.cevt.hmi.com.musicplayerapp D/Test: java.lang.NullPointerException: Attempt to invoke virtual method 'void poc.cevt.hmi.com.musicplayerapp.AllSongs.addSong(poc.cevt.hmi.com.musicplayerapp.Song)' on a null object reference

I have one mp3 file inside my raw folder. The Song's first and third parameters are ID and artist respectively, I'm just trying to get the title to work for now. Any alternative/better solutions to what I have are also welcome.

allSongsnull ,因此您在调用addSong()收到NullPointerException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM