2

I'm developing an Android music player app that has a capability of saving playlists (collection of musics) to disk. However I got some questions, I have googled it but nothing can satisfy me.

Most of the tutorials/guide I found always use _ID (it has integer/long datatype as far as I can tell) to distinguish music files among each others. How unique is it?

The uniquenesses that I mean can be described like this:

  1. I found that the _ID seems like sequential numbers. What will happen if I add/delete a music, will another musics' IDs get changed?
  2. Is it safe to store my playlist data as a list of music IDs? Will the playlist get reserved with any user's changes to music files on disk?

Thanks.

akhy
  • 5,760
  • 6
  • 39
  • 60

2 Answers2

7

Although this is an old question, the above answer is incorrect. Simply deleting or adding new music will be fine as Android will assign it auto-incremental _id. However, when you simply move a file(in your case, a song), the _id column changes values! It's almost like the MediaStore deletes the entry first and then creates a new entry.

So, for your point 2, those ID's could become stale over a period of time when users move their songs around.

Kedar Paranjape
  • 1,822
  • 2
  • 22
  • 33
2

_IDs are unique. Just like databases the ID is usually autoincremented. So if a item is added it will use the previous added ID + 1. So if you delete an item, the other IDs in the database are not changed (so there will be gaps => no worries)

So for question 2, it's safe.

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • 5
    Wrong. Moved files will be assigned a new `_id` value. They do _not_ retain their old IDs. – Kedar Paranjape Feb 03 '14 at 04:08
  • @black_stallion how is this related? – RvdK Feb 08 '14 at 20:39
  • 1
    @RvdK Lets assume the OP creates a playlist containing a song with `_ID=3599` and stores it in his app's database. Somebody later moves the song in the filesystem. When that same playlist is fetched again from his DB, the `ID=3599` will probably not exist at all (and even if it does, there's a high probability that it's a different song). This is because Android changes the `_ID` value of the entry of a (moved) file in the `MediaStore`. Obviously for the OP's second question, he cannot rely on the `_ID` column to guarantee "correctness" of a playlist. – Kedar Paranjape Feb 09 '14 at 11:53
  • 4
    Thats indeed trough, if the sdcard(which has the music on it) gets disconnected and connected, all the songs will have **new** ID, so your old playlist becomes not valid anymore (it cannot find the songs with the old IDs) – RvdK Mar 02 '14 at 08:55