如何从媒体库获取歌曲?

3

我在模拟器中有9首歌,列表视图中显示的项目数为9项。好极了,太好了!!!唯一的问题是这9个项目都是同一首歌。我已经在这个网站上找了很久,但还没有找到解决方案。下面的代码是我用来查询媒体存储的。

 package javierpech.codeit.xaverius;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class queryMediaStore {

    private ArrayList<HashMap<String, String>> songs= new ArrayList<HashMap<String, String>>();
    private String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    private Uri externalUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    private Cursor cursor;
    private String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
    private String[] projection = {
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
        //    MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DATA,
        //  MediaStore.Audio.Media.DURATION
    };          

    //PUBLIC CONSTRUCTOR
    public queryMediaStore(){

    }

    public ArrayList<HashMap<String, String>> updatePlaylist(Context c){

        HashMap<String, String> tempSong = new HashMap<String, String>();

        cursor = c.getContentResolver().query(
                externalUri,
                projection,
                selection,
                null,
                sortOrder);
        if (cursor!= null)
        {
            if(cursor.moveToFirst()){
                while(cursor.moveToNext()){

                    tempSong.put("songArtist", cursor.getString(0));
                    tempSong.put("songTitle", cursor.getString(1));
                    tempSong.put("songPath", cursor.getString(2));

                    songs.add(tempSong);
                } 
            }
        }cursor.close();

        return songs;
    }
}
1个回答

0
尝试使用这个来获取歌曲。
   private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


public ArrayList<HashMap<String, String>> getPlayList(Context c) {




    final Cursor mCursor = c.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaColumns.TITLE, MediaColumns.DATA, AudioColumns.ALBUM }, null, null,
            "LOWER(" + MediaColumns.TITLE + ") ASC");

    String songTitle = "";
    String songPath = "";





    /* run through all the columns we got back and save the data we need into the arraylist for our listview*/
    if (mCursor.moveToFirst()) {
        do {


            songTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));

            songPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.DATA));
            HashMap<String, String> song = new HashMap<String, String>();

            song.put("songTitle", songTitle);
            song.put("songPath", songPath);


            songsList.add(song);

        } while (mCursor.moveToNext());

    }   

    mCursor.close(); //cursor has been consumed so close it
return songsList;
}

然后这个用于将歌曲添加到列表中

       public class PlayListActivity extends ListActivity  {

// Songs list
public ArrayList<HashMap<String,String>> songsList = new ArrayList<HashMap<String, String>>();
ListView musiclist;
 Cursor mCursor;
 int songTitle;
 int count;
 int songPath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    ArrayList<HashMap<String, String>> tempSong = new ArrayList<HashMap<String, String>>();


  SongsManager plm = new SongsManager();


    // get all songs from sdcard
    this.songsList = plm.getPlayList(this);

    // looping through playlist
    for (int i = 0; i < songsList.size(); i++) {
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);

        // adding HashList to ArrayList
        tempSong.add(song);

    }

    // Adding menuItems to ListView
   ListAdapter adapter = new SimpleAdapter(this, tempSong,
                     android.R.layout.simple_list_item_1, new String[] {      "songTitle", "songPath" }, new int[] {
                     android.R.id.text1});

              setListAdapter(adapter);


    // selecting single ListView item
    ListView lv = getListView();

    // listening to single listitem click
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // getting listitem index
            int songIndex = position;
            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                   MainActivity.class);
            Log.d("TAG","onItemClick");
            // Sending songIndex to PlayerActivity
            in.putExtra("songPath", songIndex);
            setResult(100, in);
            // Closing PlayListView
            finish();
    }

 });
} 

可能有人有更好的方法来做这件事,但我百分之百确定这个方法是有效的!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接