gpt4 book ai didi

android - 如何从媒体商店获取歌曲?

转载 作者:太空狗 更新时间:2023-10-29 15:05:59 25 4
gpt4 key购买 nike

我的模拟器中有 9 首歌曲, ListView 中显示的项目数为 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;
}
}

最佳答案

试试这个来获取歌曲

   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();
}

});
}

可能有人有更好看的方法,但我 100% 知道这行得通!

关于android - 如何从媒体商店获取歌曲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com