[英]How to change path to assets folder?
im developing an audio player that reads all mp3 files from Sdcard and then plays it, right now i need to change its path, i want it to read mp3 files in assets folder, how can this be done ? 我正在开发一个音频播放器,该音频播放器可以从Sdcard读取所有mp3文件,然后播放它,现在我需要更改其路径,我希望它读取Assets文件夹中的mp3文件,该怎么办?
this is the code 这是代码
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"));
}
}
} }
Write it in the data directory : Android - Creating a folder in the data/data/pkg/files directory 将其写入data目录: Android-在data / data / pkg / files目录中创建一个文件夹
If I remember it is /data/data/your_package/ (you can see it in DDMS perspective of Eclipse) 如果我记得它是/ data / data / your_package /(您可以在DDMS Eclipse透视图中看到它)
Try this code: 试试这个代码:
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.