Android如何从已知的URI获取单个歌曲的ID信息

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

    }
}

成功了!感谢您的跟进-- - kittka

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

这段代码是从这篇文章中提供的 右边:-)


这段代码似乎查询了所有的音乐。问题明确要求使用特定的URI。如果您认为这段代码是合适的,请解释一下,不要只是随意粘贴代码。 - Vince
@Vince,你说得对,它确实返回了所有结果,并且绝不是随机的。我今天会进行更新以更好地适应问题。 - App Dev Guy

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