0

Reference: In PlayMusic app as soon as a song is added the library is automatically updated even when the app is running.

How can the same be implemented for building a new music player app?

I am using MediaStore.Audio.Media to get the initial list of all the songs.

Community
  • 1
  • 1
Ankur Jain
  • 507
  • 3
  • 9

2 Answers2

3

You are accessing a Content Provider and can use it's API to register notification upon changes.

Use the ContentResolver.registerContentObserver() method from activity, and supply the base Content URI for the provider and pass true for notifyForDescendents parameter, and your callback will get hit every time there's a change.

getContentResolver().registerContentObserver(MediaStore.​Audio.Media.EXTERNAL_CONTENT_URI, true, observer);

Pro tip: register and unregister in onResume() and onPause() respectively or you will get a crash if there are any changes while your app is not in the foreground.

mach
  • 8,315
  • 3
  • 33
  • 51
  • 1
    getApplicationContext().getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, observer); Can you also tell when should I unregister. – Ankur Jain Mar 12 '15 at 10:11
-1
  • You can check with regular time interval set with timer for eg. after every 2 mins update the file for your media player.
  • You can also manage during your lifecycle callback of onResume, onPause with a separate thread to check for any new added songs.

I would recommend second approach.

You can also check the source for default music player in android Music player before android ICS Hope it helps:

Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40