首先我是一个新手,这是在customlost中添加歌曲名称的代码,我需要的只是使用mediastore的一些功能(我尝试过metadataretriever,但应用程序停止响应)这样我也可以添加它们...... .
公共类 FragmentSongs 扩展 Fragment {
private MediaPlayer mp =new MediaPlayer();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
ListView SngList = (ListView) view.findViewById(R.id.SongList);
File f=new File("/sdcard");
ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);
if (Songinfo.size()>0){
SngList.setAdapter(new CustomAdapter(Songinfo));
return view;
}
else return null;
}
public ArrayList<SongDetails> getSongsFromDirectory(File f)
{// MediaMetadataRetriever metaRetriever = 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++)
{
if (files[i].isFile()){
// metaRetriever.setDataSource(files[i].getName());
//metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
SongDetails detail=new SongDetails();
detail.setIcon(R.drawable.ic_launcher);
String fileName = files[i].getName();
detail.setSong(files[i].getName());
detail.setArtist(files[i].getName());
detail.setAlbum(files[i].getName());
songs.add(detail);
}else if (files[i].isDirectory()){
songs.addAll(getSongsFromDirectory(files[i]));
}
}
return songs;
} }
class Mp3Filter implements FileFilter{
public boolean accept(File file){
return (file.isDirectory()||file.getName().endsWith(".MP3")|| file.getName().endsWith(".mp3"));
}