Search code examples
androidmedia-playeruri

Android How to get a single song's ID info from a know URI


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

Solution

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