0

I want to get all the musics which are not in a given playlist using CursorLoader. Is it possible?

We can get all the music in a list using the following code

loader = new CursorLoader(context, MediaStore.Audio.Playlists.Members
                .getContentUri("external", playlistID), proj,
                MediaStore.Audio.Media.IS_MUSIC + " != 0 ", null, ORDER_BY);

But I need to do the opposite, which is to get all the music that are not in a given playlist. How can I do that?

Imon
  • 3,905
  • 4
  • 25
  • 25
  • I think something should work as a selectionArgs – Imon May 22 '12 at 09:41
  • Is there a way for subtracting the content of a cursor from another cursor? Like allSongsCursor - singlePlaylistCursor..... – Imon May 22 '12 at 09:42

1 Answers1

1

I can't help you fix your LowAcceptRateException - that's a critical failure around here that's likely to interfere with questions being answered in the long run - but if you do not mind doing stuff that is hard to maintain and abuses internal functionality in the MediaProvider you can do like this to get songs that are not in a (specific) playlist.

loader = new CursorLoader(context, Audio.Media.EXTERNAL_CONTENT_URI, proj,
            Media._ID + " NOT IN (SELECT audio_id FROM audio_playlists_map WHERE playlist_id=?) AND " + 
            MediaStore.Audio.Media.IS_MUSIC + " != 0 ", 
            new String[]{String.valueOf(playlistID}, ORDER_BY);

Edit: Corrected audio_playlist_map to audio_playlists_map

Jens
  • 16,853
  • 4
  • 55
  • 52
  • Thanks! Where do I find that audio_playlist_map table? – Imon May 22 '12 at 13:07
  • Receiving error ---05-22 18:36:37.161: E/DatabaseUtils(513): android.database.sqlite.SQLiteException: no such table: audio_playlist_map: , while compiling: SELECT _id, title, _data, _display_name, album, artist, composer, year, album_id, _size FROM audio WHERE (_id NOT IN (SELECT audio_id FROM audio_playlist_map WHERE playlist_id=?) AND is_music != 0 ) ORDER BY title COLLATE NOCASE ASC – Imon May 22 '12 at 13:31
  • Oh right, yeah, it's probably called `audio_playlists_map` actually. Missed an `s`. That's what happens when you write SQL from memory. – Jens May 22 '12 at 13:51
  • Hey Jens! Would you please take a look at my question -http://stackoverflow.com/questions/6273592/content-provider-install-failed-conflicting-provider-installing-content-provide – Imon May 22 '12 at 14:02
  • Either use a *different authority* for each of the providers and detect the presence when installing the second app - and adjust the authority in the `Uri`s you use to call the ContentProvider - i.e. "user installs App1, App1 does not detect App2 and configures itself to use authority #1 in all subsequent calls" - then "user installs App2, App2 finds App1 and configures itself to use authority #1 in all subsequent calls" - i.e. make the `Uri`s variable. This poses a problem when the user uninstalls `App1` however - his data will be lost in `App2` also. – Jens May 22 '12 at 14:15
  • But it works well if I install the provider as a different apk! Is there no way to supply 2 apks as one or in a single package in Android Market? – Imon May 22 '12 at 14:38
  • Nope not that I know of - short of launching Google Play/Market & requesting that the user installs your other "provider" app if it's missing. – Jens May 22 '12 at 14:39