首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从mediastore获取歌曲?

如何从mediastore获取歌曲?
EN

Stack Overflow用户
提问于 2014-02-25 05:19:38
回答 1查看 1.3K关注 0票数 3

我的模拟器中有9首歌曲,列表视图中显示的项目数是9。唯一的问题是这9个项目是同一首歌。我一直在这个网站上转,但没有找到解决我的问题的方法。下面的代码是我用来查询媒体存储的代码。

代码语言:javascript
复制
 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;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-04-02 23:51:13

试试这首歌吧

代码语言:javascript
复制
   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;
}

然后将这些歌曲添加到列表中

代码语言:javascript
复制
       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();
    }

 });
} 

也许有人会用一种更好看的方式来做这件事,但我100%地知道这是有效的!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21999229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档