[英]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. 我正在开发一个可以播放MP3文件的应用。 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.
我想使用mp3文件的名称作为标题将原始文件夹中的mp3文件转换为歌曲对象的ArrayList。 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. 我的原始文件夹中有一个mp3文件。 The Song's first and third parameters are ID and artist respectively, I'm just trying to get the title to work for now.
这首歌的第一个和第三个参数分别是ID和artist,我只是想让标题暂时可用。 Any alternative/better solutions to what I have are also welcome.
也欢迎任何其他替代/更好的解决方案。
allSongs
为null
,因此您在调用addSong()
收到NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.