2

首先我是一个新手,这是在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"));
    }
4

1 回答 1

1

不确定确切的问题是什么,但如果您想将歌曲标题添加到自定义列表中,为什么不使用这样的东西

    final String track_id = MediaStore.Audio.Media._ID; 
    final String track_no =MediaStore.Audio.Media.TRACK;
    final String track_name =MediaStore.Audio.Media.TITLE;
    final String artist = MediaStore.Audio.Media.ARTIST;
    final String duration = MediaStore.Audio.Media.DURATION;
    final String album = MediaStore.Audio.Media.ALBUM;
    final String composer = MediaStore.Audio.Media.COMPOSER;
    final String year = MediaStore.Audio.Media.YEAR;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
public Cursor getTrackTrackcursor(Context context, Cursor cursor)
{
    // gets all tracks
    ContentResolver cr =  context.getContentResolver();
    final String[]columns={track_id, track_no, artist, track_name,album, duration, path, year, composer};
    cursor = cr.query(uri,columns,null,null,null);
    return cursor;
}

希望这可以帮助

于 2013-08-04T09:42:34.283 回答