Android Cómo obtener la información de identificación de una sola canción de un URI conocido

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

preguntado el 29 de julio de 12 a las 01:07

2 Respuestas

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

    }
}

Respondido 01 ago 12, 20:08

It did! Thank you for the follow up -- - Kittka

Thanks, glad I could help. - user1234051

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 :-)

contestado el 23 de mayo de 17 a las 12:05

This code appears to query ALL the music. The question explicitly asks for using a specific URI. If you think this code is appropriate, please explain it, don't just paste random code. - Vince

@Vince you are correct, it does return all results, and was not random by any means. I will update to suit the question better today. - Desarrollador de aplicaciones

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.