首先,我是一个新手,我想知道在mediametadatareverver中设置什么样的uri? 我可以将其设置为metaRetriever.setDataSource(files [i] .getName());如果不能将正常路径转换为uri路径的方法是什么?
我应该用????替换这个(files [i] .getName())
这是代码
public ArrayList<SongDetails> getSongsFromDirectory(File f)
{MediaMetadataRetriever mmr = new MediaMetadataRetriever();
ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
if (!f.exists() || !f.isDirectory())
{
return songs;
}
File[] files = f.listFiles(new Mp3Filter());
for(int i=0; i<files.length; i++)
{
Uri uri = Uri.fromFile(files[i]);
//mmr.setDataSource(null, uri);
if (files[i].isFile()){
SongDetails detail=new SongDetails();
detail.setIcon(R.drawable.ic_launcher);
detail.setSong(files[i].getName());
//detail.setArtist(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
//detail.setAlbum(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
songs.add(detail);
}else if (files[i].isDirectory()){
songs.addAll(getSongsFromDirectory(files[i]));
}
}
return songs;
} }
答案 0 :(得分:0)
此代码应该有效:
File [] files = ... // your initialized variable
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
for (int i = 0; i < files.length; i++) {
Uri uri = Uri.fromFile(files[i]);
mmr.setDataSource(this, uri);
String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
// More code, do something with the metadata, etc...
}
mmr.release();