0

我正在开发一个音频播放器,它从 Sdcard 读取所有 mp3 文件然后播放它,现在我需要更改它的路径,我希望它读取 assets 文件夹中的 mp3 文件,这怎么做?

这是代码

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/");
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

2 回答 2

0

写入数据目录:Android - 在data/data/pkg/files目录下创建文件夹

如果我记得它是 /data/data/your_package/ (你可以在 Eclipse 的 DDMS 透视图中看到它)

于 2013-06-01T01:21:49.163 回答
0

试试这个代码:

AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();
于 2014-11-05T20:01:52.643 回答