方法一:getAssets().openFd
//读取asset内容
private void openAssetMusic(String index) throws IOException {
// String fileName = "a001.mp3"; //根目录文件
String fileName = index + "/" + index + ".mp3"; //子目录文件
AssetFileDescriptor fd = getAssets().openFd(fileName);
mPlayer = new MediaPlayer();
mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
mPlayer.prepare();
}
方法二:ResourceUtil.getResId(index, R.raw.class)
//读取raw文件
private void openRawMusic(String index) throws IOException {
int resourceId = ResourceUtil.getResId(index, R.raw.class);//读取raw文件
mPlayer = MediaPlayer.create(mContext, resourceId);
}
其中方法二为了读取raw文件夹下资源,用到了反射类 ResourceUtil.java
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10657628.html
转载请著名出处!谢谢~~
原文地址:https://www.cnblogs.com/wukong1688/p/10657628.html