I'm working with Android on a Media Player app, when creating a playlist in the database and then try to modify it, like removing songs from the playlist or moving song to other position(the app has a reordering feature, drag and drop), it simply doesn't work. I'm using these two codes for removing and reordering:
public boolean movePlaylistSong(int playlistId, int from, int to){
try{
return MediaStore.Audio.Playlists.Members.moveItem(context.getContentResolver(), playlistId, from, to);
}catch(Exception e){
Logger.e(TAG, e.getMessage());
}
return false;
}
public boolean removeFromPlaylist(int playlistId, int audioId) {
try{
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
return resolver.delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID +"=?", new String[]{String.valueOf(audioId)}) != 0;
}catch(Exception e){
e.printStackTrace();
}
return false;
}
They both return true that it was successful but when reloading the playlist from the database(The external content uri of the playlists) again it returns the original one with NO CHANGES applied. It returns success result but it actually didn't work.
Thanks in advance.
Calling moveItem will change the PLAY_ORDER value, but you need to sort by the PLAY_ORDER on your cursor to see the change. Otherwise the cursor will sort by _ID, which doesn't get edited.
Uri uriPlaylistTracks = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID);
String sortOrder = MediaStore.Audio.Playlists.Members.PLAY_ORDER;
cursor = resolver.query(uriPlaylistTracks, STAR, null, null, sortOrder);
To change the PLAY_ORDER, I did this:
//get the PLAY_ORDER values for song
cursor.moveToPosition(fromPosition);
int from = (int) cursor.getLong(cursor.getColumnIndex(Audio.Playlists.Members.PLAY_ORDER));
cursor.moveToPosition(toPosition); //position in list
int to = (int) cursor.getLong(cursor.getColumnIndex(Audio.Playlists.Members.PLAY_ORDER));
//update the PLAY_ORDER values using moveItem
boolean result = MediaStore.Audio.Playlists.Members.moveItem(resolver,
playlistID, from, to);
//get new cursor with the updated PLAY_ORDERs
cursor = resolver.query(uriPlaylistTracks, STAR, null, null, sortOrder);
//change the cursor for a listview adapter
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged();
Struggling with same issue. Behaves as successful but no change. I did note however that your playlistid is of type int yet the documentation states long.
public static final boolean moveItem (ContentResolver res, long playlistId, int from, int to)
I also found this answer on the forum
Alternative to MediaStore.Playlists.Members.moveItem
I found that it does move the PLAY_ORDER but not the AUDIO_ID.
Related
My app displays playlists where for each playlist I show albumart in recyclerview.
A custom adapter displays the rows. It all works fine but when I add another playlist, using an asynctask, the display gets redrawn several times. I have checked that there are no adapter.notifydatasetchanged() calls. Stepping through the code I have discovered that it happens when there is a resolver.insert
In this example, the routine creates a new playlist, either for whole albums or tracks.
public void addTracksToPlaylist(Context context, String music_id,
long playlist_id, String nmode, int base) {
// feed it the album_id or track_id
Uri mediauri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
Uri exturi = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlist_id);
String[] projection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TRACK};
String where = MediaStore.Audio.Media.ALBUM_ID + " =?";
String orderBy = null;
if (nmode.equals(context.getString(R.string.track_mode))) {
where = MediaStore.Audio.Media._ID + "=?";
}
orderBy = MediaStore.Audio.Media.ARTIST + " ASC, "
+ MediaStore.Audio.Media.ALBUM + " ASC , " +
"CAST(" + (MediaStore.Audio.Media.TRACK) + " AS INTEGER) ASC";
String[] whereVal = {music_id};
Cursor c = resolver.query(mediauri, projection, where, whereVal,
orderBy);
if (c != null && c.moveToFirst()) {
int idColumn = c.getColumnIndex(MediaStore.Audio.Media._ID);
ContentValues values = new ContentValues();
boolean stamp = prefs.getstampSelected(context);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String audio_id = c.getString(idColumn);
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);
try {
resolver.insert(exturi, values);
} catch (Exception e) {
e.printStackTrace();
}
// String thistrack= track.getfullPathfromAudioId(context,audio_id);
// Log.i(TAG,"position : "+base +" Inserted track = "+ audio_id + " track "+thistrack+ " Album_id = "+ music_id);
base++;
if (stamp) {
try {
track.updateTrackModifiedDate(audio_id, context);
} catch (Exception e) {
e.printStackTrace();
}
}
}
c.close();
}
}
What can I do to prevent these actions, which happen in the background, affect the display of playlists. Or in other words, why/how is my recyclerView "active" as it appears to be aware of changes in the database.
Update:
From the Android Developers website:
The Loader will monitor for changes to the data, and report them to you through new calls here. You should not monitor the data yourself. For example, if the data is a Cursor and you place it in a CursorAdapter, use the CursorAdapter(android.content.Context, android.database.Cursor, int) constructor without passing in either FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0 for the flags argument). This prevents the CursorAdapter from doing its own observing of the Cursor, which is not needed since when a change happens you will get a new Cursor throw another call here.
the monitoring and reporting back by the loader manifests itself in onLoadFinished calls which in my case reset the adapter.
As was stated in the comments, the Loader monitors changes in the underlying data. The solution was simple in the end.
I declare a boolean variable
private boolean processing=false;
Set the variable to true in the onPreExecute() and to false in onPostExecute() of the AsyncTask.
As a change fires the OnLoadFinished call I test for the value of processing and skip the code. Once completed, I call getLoaderManager().restartLoader etc
I have 2 separate methods for retrieving songs using the content resolver. One i use to get all the songs on the phone and one i use to retrieve all playlists from my phone and then, for each playlist i get the songs on it and add it to the playlist object. I do this by searching through the list of all the songs for the ids that match the ids in the playlist. The problem is that the ids don't match. I know for sure for ex. that one playlist has 3 songs on it but the ids it finds are totally different from the one of the songs.
This is the method i use for retrieving all the songs:
public ArrayList<Song> getSongList(){
ArrayList<Song> songs = new ArrayList<Song>();
//query external audio
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
//iterate over results if valid
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int albumColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM);
int durationColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
int displayNameColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DISPLAY_NAME);
//add songs to list
do {
String name;
if(musicCursor.getString(titleColumn)==null || musicCursor.getString(titleColumn).isEmpty()) name=musicCursor.getString(displayNameColumn);
else name=musicCursor.getString(titleColumn);
songs.add(new Song(musicCursor.getLong(idColumn), name, musicCursor.getString(albumColumn), musicCursor.getString(artistColumn), musicCursor.getString(durationColumn), musicCursor.getString(dataColumn)));
}
while (musicCursor.moveToNext());
}
And this is the one for playlists:
public void getPlayists() {
playlists = new ArrayList<Playlist>();
// query external audio
String[] projection = {
android.provider.MediaStore.Audio.Playlists._ID,
android.provider.MediaStore.Audio.Playlists.NAME };
String[] projectionPl = {
android.provider.MediaStore.Audio.Media._ID,
android.provider.MediaStore.Audio.Media.TITLE };
ContentResolver resolver = context.getContentResolver();
Uri playlistsUri = android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
Cursor playlistsCursor = resolver.query(playlistsUri, projection, null,
null, null);
// iterate over results if valid
if (playlistsCursor != null && playlistsCursor.moveToFirst()) {
// get columns
int idColumn = playlistsCursor
.getColumnIndex(android.provider.MediaStore.Audio.Playlists._ID);
int nameColumn = playlistsCursor
.getColumnIndex(android.provider.MediaStore.Audio.Playlists.NAME);
// add songs to list
do {
long id = playlistsCursor.getLong(idColumn);
Playlist pl = new Playlist(
playlistsCursor.getString(nameColumn), id);
Cursor plCursor = resolver.query(
android.provider.MediaStore.Audio.Playlists.Members
.getContentUri("external", id), projectionPl,
null, null, null);
if (plCursor != null && plCursor.moveToFirst()) {
// get columns
int idColumna = plCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int nameColumna = plCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
// add songs to list
do {
long ida = plCursor.getLong(idColumna);
String title = plCursor.getString(nameColumna);
for (Song song : songs) {
if (song.id == ida) {
pl.addSong(song);
}
}
} while (plCursor.moveToNext());
}
playlists.add(pl);
} while (playlistsCursor.moveToNext());
}
Can someone explain exactly how the song ids work or how can i modify my methods to make them work? Thanks.
Your playlistsCursor is against android.provider.MediaStore.Audio.Playlists.Members whereas you then proceed to get values from playlistsCursor but against android.provider.MediaStore.Audio.Media
The returned structure for a playlist is:
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, playorder);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);
so if you are looking for the audio Id then change
int idColumn = playlistsCursor
.getColumnIndex(android.provider.MediaStore.Audio.Playlists._ID);
to
int idColumn = playlistsCursor
.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID);
ps: I have previously posted code to create playlists etc.
Look at my app Playlist Manager on Google Play or Amazon App Store for additional functionality
My application uses the Contacts ContentProvider to store some of its data. When I load a contact into memory, I want to save its ID (so that I know how to save changes later), and the ID of all data fields it is using (so they can be directly updated). Here is some of my code:
Uri entityUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, id),
Entity.CONTENT_DIRECTORY);
Cursor resultData = context.getContentResolver().query(
entityUri,
new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
null, null, null);
resultData.moveToFirst();
this.id = id;
while (resultData.isAfterLast() == false) {
this.source_id = resultData.getInt(0);
if (!resultData.isNull(1)) {
if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) {
this.display_name = resultData.getString(3);
this.display_name_id = resultData.getInt(1);
}
}
resultData.moveToNext();
}
resultData.close();
return this;
That queries the ContentProvider and gets the DISPLAY_NAME field from the data. The ID of the data record is stored in the display_name_id variable. It comes out as 4612 when I run it on my device.
I tried saving it, but it does not update as expected. In order to debug, I added a query that tries to find the correct data table record.
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.Data._ID, CommonDataKinds.StructuredName.DISPLAY_NAME},
ContactsContract.Data._ID + "=?",
new String[] {String.valueOf(this.display_name_id)}, null);
However, this cursor comes back as having a length of 0. How can this be? Why is the ID incorrect?
I tried locally and it works for me, here's my code slightly adapted from yours:
public void testContacts(final #Nonnull Context context, final int rawContactId, final #Nonnull String expectedDisplayName) {
Uri entityUri = Uri.withAppendedPath(
ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId),
ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY);
Cursor resultData = context.getContentResolver().query(
entityUri,
new String[]{
ContactsContract.RawContacts.SOURCE_ID,
ContactsContract.RawContacts.Entity.DATA_ID,
ContactsContract.RawContacts.Entity.MIMETYPE,
ContactsContract.RawContacts.Entity.DATA1
},
null, null, null);
int displayNameId = -1;
try {
final int columnIndexDataId = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.DATA_ID);
final int columnIndexMimetype = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.MIMETYPE);
final int columnIndexData = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.DATA1);
while (resultData.moveToNext()) {
if (!resultData.isNull(columnIndexDataId)) {
if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE.equals(resultData.getString(columnIndexMimetype)) &&
expectedDisplayName.equals(resultData.getString(columnIndexData))) {
displayNameId = resultData.getInt(1);
break;
}
}
}
} finally {
resultData.close();
}
String reLookedUpDisplayName = null;
if (displayNameId != -1) {
Cursor reLookupCursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[] {
ContactsContract.Data._ID,
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
},
ContactsContract.Data._ID + "=?",
new String[] {String.valueOf(displayNameId)},
null);
try {
final int columnIndexId = reLookupCursor.getColumnIndex(ContactsContract.Data._ID);
final int columnIndexDisplayName = reLookupCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
while (reLookupCursor.moveToNext()) {
reLookedUpDisplayName = reLookupCursor.getString(columnIndexDisplayName);
}
} finally {
reLookupCursor.close();
}
}
Toast.makeText(
context,
reLookedUpDisplayName != null ? "Found re-looked up name: " + reLookedUpDisplayName : "Didn't find name re-looking it up",
Toast.LENGTH_LONG)
.show();
}
There's no big difference from your code, so compare or try to replace bits of it to see where you have a problem. Make sure you use a fresh Cursor for each query, and close it correctly afterwards (in a finally clause).
Another thing, make sure that if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) is really what you're wanting to do (it compares the entry mime type with Fields.DISPLAY_NAME), but since you're saying you get the data ID correctly this shouldn't be the problem.
I'm trying to retrieve the metadata from a video file (title, language, artist) using the method MediaStore.Video.query(). However, the method is always returning null. The code is bellow:
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
Cursor cursor = MediaStore.Video.query(getApplicationContext().getContentResolver(), videoUri,columns);
if (cursor != null) {
cursor.moveToNext();
}
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Any suggestion about how to return video metadata using android?
==Update
As I searched in many places, I tried one solution using CursorLoader. However, the method loadInBackground() from CursorLoader is also returning null. The code is showed bellow:
String[] columns = {
MediaStore.Video.VideoColumns.TITLE
};
Uri videoUri = Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4");
CursorLoader loader = new CursorLoader(getBaseContext(), videoUri, columns, null, null, null);
Cursor cursor = loader.loadInBackground();
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4") is not an Uri for MediaStore. It would try to find a ContentProvider for authority mnt which does not exist.
MediaStore can handle only content://media/... Uris which you should get exclusively via MediaStore, not by using Uri.parse().
In your case use the following for example
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
String selection = MediaStore.Video.VideoColumns.DATA + "=?";
String selectionArgs[] = { "/mnt/sdcard/Movies/landscapes.mp4" };
Cursor cursor = context.getContentResolver().query(uri, columns, selection, selectionArgs, null);
The MediaStore.Video.VideoColumns.DATA field holds the path to the videos and you search for a certain video this way. At least for now, future versions of Android may change that.
Your second example is using CursorLoader the wrong way. If you call loader.loadInBackground() yourself, you load the data in foreground. See e.g. http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/
The next thing you do is
Cursor cursor = getCursor();
cursor.moveToFirst();
String title = cursor.getString(/* some index */);
This will lead to a CursorIndexOutOfBoundsException if your cursor has 0 rows and cursor.moveToFirst() failed because there is no first row. The cursor stays before the first row (at -1) and that index does not exist. That would mean in your case that the file was not found in the database.
To prevent that use the return value of moveToFirst - it will only be true if there is a first row.
Cursor cursor = getCursor(); // from somewhere
if (cursor.moveToFirst()) {
String title = cursor.getString(/* some index */);
}
A more complete example including checks for null and closing the cursor in all cases
Cursor cursor = getCursor(); // from somewhere
String title = "not found";
if (cursor != null) {
if (cursor.moveToFirst()) {
title = cursor.getString(/* some index */);
}
cursor.close();
}
I guess the file you try to find is either not indexed in the database (rebooting forces the indexer to run again) or the path is wrong.
Or the path you use is actually a symlink in which case MediaStore might use a different path.
Use this to get rid of symlinks
String path = "/mnt/sdcard/Movies/landscapes.mp4";
try {
path = new File(path).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
Yes, I tested now and it is throwing IndexOutOfBoundsException. When I'm using cursor.getColumnCount() it returns 1
cursor.getColumnCount() is the column count, not the row count. It should always be the same as the number of columns you requested in columns. You need to check cursor.getCount() if you want to check the row count.
Try dumping all the videos known to MediaStore into logcat in case it does not show as expected.
public static void dumpVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
vidsCount = c.getCount();
while (c.moveToNext()) {
Log.d("VIDEO", c.getString(0));
}
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}
I updated your code, it works, just check it
public static void dumpVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
c.moveToFirst();
vidsCount = c.getCount();
do {
Log.d("VIDEO", c.getString(0));
}while (c.moveToNext());
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}
I am trying to create an app which simply offers an edittext and imagebutton. If the butten gets clicked, the idea is that an album is added to the Playlist, named in the edittext box. Albums should be selected randomly. Goes without saying that the album tracks should be in the correct order.
I can add more functionality later eg. save, overwrite, delete etc.
I have the interface but am struggling with the code. I sort of get the concept of ContentProviders.
so the code needs to:
access the Playlists, which I believe is achieved by using MediaStore.Audio.Playlists
access the Albums, which I believe is achieved by using MediaStore.Audio.Albums
add to the Playlist
I have the following code (most bits obtained from this site. Thanks btw) to access the Playlist but it crashes with a Null Exception error.
public void checkforplaylists()
{
//Get a cursor over all playlists.
final ContentResolver resolver= MediaProvider.mContentResolver;
final Uri uri=MediaStore.Audio.Playlists.INTERNAL_CONTENT_URI;
final String id=MediaStore.Audio.Playlists._ID;
final String name=MediaStore.Audio.Playlists.NAME;
final String[]columns={id,name};
final Cursor playlists= resolver.query(uri, columns, null, null, null);
if(playlists==null)
{
Log.e(TAG,"Found no playlists.");
return;
}
return;
}
Anyone who can help?
I think you mean NullPointerException, which means one of your assignments is null and then you try to access a member of the object you intended it to be. Most likely it is resolver, but to be sure you need the line number reported and/or to step through that with a debugger.
This works. When using the ContentResolver, the Context (this) is required.
public void checkforplaylists(Context context)
{
ContentResolver cr = context.getContentResolver();
final Uri uri=MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String id=MediaStore.Audio.Playlists._ID;
final String name=MediaStore.Audio.Playlists.NAME;
final String[]columns={id,name};
final Cursor playlists= cr.query(uri, columns, null, null, null);
if(playlists==null)
{
Log.e(TAG,"Found no playlists.");
return;
}
Log.e(TAG,"Found playlists.");
return;
}
use this code, will work
public boolean addPlaylist(String pname) {
Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
Cursor c = resolver.query(playlists, new String[] { "*" }, null, null,
null);
long playlistId = 0;
c.moveToFirst();
do {
String plname = c.getString(c
.getColumnIndex(MediaStore.Audio.Playlists.NAME));
if (plname.equalsIgnoreCase(pname)) {
playlistId = c.getLong(c
.getColumnIndex(MediaStore.Audio.Playlists._ID));
break;
}
} while (c.moveToNext());
c.close();
if (playlistId != 0) {
Uri deleteUri = ContentUris.withAppendedId(playlists, playlistId);
Log.d(TAG, "REMOVING Existing Playlist: " + playlistId);
// delete the playlist
resolver.delete(deleteUri, null, null);
}
Log.d(TAG, "CREATING PLAYLIST: " + pname);
ContentValues v1 = new ContentValues();
v1.put(MediaStore.Audio.Playlists.NAME, pname);
v1.put(MediaStore.Audio.Playlists.DATE_MODIFIED,
System.currentTimeMillis());
Uri newpl = resolver.insert(playlists, v1);
Log.d(TAG, "Added PlayLIst: " + newpl);
flag=true;
return flag;
}