首先,我对Java编程完全陌生(3周前才开始),我尽力解决了两天。但没有成功。
我有这个从 SD 卡读取 mp3 文件的类:
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/audio");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
问题:我的应用程序包含 4 个类,并且都在使用ArrayList< HashMap< String, String >>
. 所以,我需要继续使用ArrayList< HashMap< String, String >>
.
必须:getPlayList()
返回类型应该总是ArrayList< HashMap< String, String >>
。因为我在其他类上使用该方法。
目标:我想在SongsManager
类中创建一个音频文件数组(存储在原始文件夹中):
int[] lessonClips = { R.raw.button_1, R.raw.button_2, R.raw.button_3, R.raw.button_4, R.raw.button_5, R.raw.button_6 };
并让 MusicPlayer 读取这个数组而不是从路径中读取。可以自定义此类以仅播放该课程剪辑。
问题:如何修改 getPlayList 以操作LessonClips
并仍然返回 asongList
类型ArrayList< HashMap< String, String >>
?