Android How to get a single song's ID info fro

2019-03-30 02:43发布

问题:

I know the URI of a single song. How do I get the song's info, title track, ect.

Sorry all posts I can find provide a method to index the entire library, not how to get a single know URI song's info. This is what I am using to get the URI. audioID is being stored in a SharedPrefs. Thanks

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

回答1:

I answered my own question, hopefully this helps someone else in the future.

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

    }
}


回答2:

The answer above just couldn't get the ball going for me unfortunately, however I discovered this for anyone still looking.

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

This code is courtesy from this post right here :-)