请有人帮我从原始或资产文件夹中播放这个Android应用播放音频/视频文件,而不是从/ SDCard /播放。
我在下面的代码中应该更改什么?
以下是Tutorial代码。
问候。
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {
// SDCard Path
@SuppressLint("SdCardPath")
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"));
}
}
}
答案 0 :(得分:0)
你可以尝试这个从Assets文件夹加载文件..
考虑资产文件夹里面有铃声文件夹然后在你的活动中使用
AssetManager assetManager = this.getResources().getAssets();
String[] files = null;
try {
files = assetManager.list("ringtone"); //ringtone is folder name
} catch (Exception e) {
Log.e(LOG_TAG, "ERROR: " + e.toString());
}
所以你从资产文件夹中获取文件,然后获取文件名并将文件名传递给你的列表
答案 1 :(得分:0)
从原始文件夹播放音频:
mediaPlayer = MediaPlayer.create(this, R.raw.song.mp3);
从资源文件夹播放音频:
AssetFileDescriptor afd = getAssets().openFd("Song.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();