1

After knowing the difference between _ID and AUDIO_ID (What is difference between _ID and AUDIO_ID column?), I'm changing my query to use AUDIO_ID instead, however I got some problem here.

Here's my code snippet that intended to query all audio files in external storage and make "Music" instances for each of them with embedded AUDIO_ID.

Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cur = mContentResolver.query(uri, null, 
    MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);

int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID);

// All of the above columns return the right integers, 
// except idColumn which returns -1 thus making my further code below throws an exception

do{
    Music music = new Music(); // it's my music class
    music.setArtist( cur.getString(artistColumn) );
    music.setTitle( cur.getString(titleColumn) );
    music.setId( cur.getLong(idColumn) ); // error here, because idColumn = -1 (column not found)

    // ......
} while (cur.moveToNext())

Why can't I get the AUDIO_ID column when querying MediaStore.Audio.Media.EXTERNAL_CONTENT_URI? Is there any way to retrieve it? (any workarounds appreciated)

I need it since my app needs absolute reference to audio files as explained in an answer comment here -> https://stackoverflow.com/a/17648131/670623

Thanks :)

Edit

I found a question with solution close to what I'm searching (it can get the AUDIO_ID), but it's intended to list all audio files from certain playlist. While my concern is querying the whole mediastore for AUDIO_IDs regardless from which playlist they are.

Given an Android music playlist name, how can one find the songs in the playlist?

Community
  • 1
  • 1
akhy
  • 5,760
  • 6
  • 39
  • 60

1 Answers1

4

I did some tests with the MediaStore.Audio.Media and MediaStore.Audio.Playlists.Members databases. I have created a small playlist with the same song contained several times and the database looks like this:

Playlist Database

+---------------------------+
|_ID | AUDIO_ID | songTitle |
+---------------------------+
|273 | 2913     | Alone     |
|274 | 1487     | Baby      |
|275 | 2913     | Alone     |
|276 | 2913     | Alone     |
|277 | 1487     | Baby      |
|278 | 2648     | Battery   |
+---------------------------+

As expected _ID is a locally unique identifier within this playlist. Of course there could be several other playlists with their own _ID identifiers. AUDIO_ID can't be used to identify a specific row in this database because the same song can appear multiple times in a single playlist.

AUDIO_ID is the globally unique identifier which is always the same for a certain song.

Now let's look at the MediaStore.Audio.Media database. In here, all songs known to the MediaStore system are stored. (this is only an excerpt)

Media Database

+-------------------+
|_ID  | songTitle   |
+-------------------+
|      ....         |
|1487 | Baby        |
|      ....         |
|2648 | Battery     |
|     ....          |
|2913 | Alone       |
|     ....          |
+-------------------+

It seems that the _ID identifier in MediaStore.Audio.Media is used as the globally unique identifier which is called AUDIO_ID in the MediaStore.Audio.Playlists.Members databases.

This seems strange at first, but every song has to be contained in MediaStore.Audio.Media and it can only appear there once. And since there is only one such database, _ID in this database should be globally unique for each song.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Alex N
  • 155
  • 3
  • 8