6

我知道一首歌曲的URI。如何获取歌曲信息、主打歌等。

抱歉,我能找到的所有帖子都提供了一种索引整个库的方法,而不是如何获取单个已知 URI 歌曲的信息。这就是我用来获取 URI 的方法。audioID 存储在 SharedPrefs 中。谢谢

int REQUEST_MEDIA = 0;
String audioID;

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult
 if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
        audioID = data.getDataString();
4

2 回答 2

7

我回答了自己的问题,希望这对将来的其他人有所帮助。

int REQUEST_MEDIA = 0;


SharedPreferences prefs;
String prefName = "MyPref";
String audioID, artist_name, artist_band;
Uri MyUri;



private OnClickListener startListener = new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult

    }
};


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
        audioID = data.getDataString();
        MyUri = Uri.parse(audioID);
        String[] proj = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Artists.ARTIST };

                Cursor tempCursor = managedQuery(MrUri,
                                            proj, null, null, null);

                tempCursor.moveToFirst(); //reset the cursor
                int col_index=-1;
                int numSongs=tempCursor.getCount();
                int currentNum=0;
                do{
                  col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                  artist_name = tempCursor.getString(col_index);
                  col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
                  artist_band = tempCursor.getString(col_index);
                  //do something with artist name here
                  //we can also move into different columns to fetch the other values

                  currentNum++;
                }while(tempCursor.moveToNext());


  prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("alarm_selected", audioID);
editor.putString("alarm_name", artist_name);
editor.putString("alarm_band", artist_band);
editor.commit();    

    }
}
于 2012-08-01T19:10:56.150 回答
0

不幸的是,上面的答案无法让我明白,但是我为仍在寻找的任何人发现了这一点。

//Some audio may be explicitly marked as not being music
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

String[] projection = {
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.ARTIST,
    MediaStore.Audio.Media.TITLE,
    MediaStore.Audio.Media.DATA,
    MediaStore.Audio.Media.DISPLAY_NAME,
    MediaStore.Audio.Media.DURATION
};

Cursor cursor = this.managedQuery(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    projection,
    selection,
    null,
    null);

private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()) {
    songs.add(cursor.getString(0) + "||" 
                + cursor.getString(1) + "||" 
                + cursor.getString(2) + "||"
                + cursor.getString(3) + "||"
                + cursor.getString(4) + "||" 
                + cursor.getString(5));

    //TO DISPLAY IN LISTVIEW 
    ListView lv = (ListView) findViewById(R.id.song_list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songs);
    lv.setAdapter(adapter);

    cursor.close();

这段代码是来自这篇文章的礼貌在这里:-)

于 2015-06-03T12:54:40.803 回答