My application worked fine when I used the original Google Music Player. It found songs and playlists, no problem. Since I started using Play Music, my application can't find any of this. Here is my code:
Cursor cursor = contentResolver.query(uri, null, MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
if (cursor == null || !cursor.moveToFirst()) {
Log.e(TAG, "Could not locate any music on device.");
return;
}
cursor.close();
Any Idea why this happens. I just got my first complaint that someon who purchased my application could not play music.
Maybe its late but i do something like this:
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.DURATION
};
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
Related
Some users told me, that my audio app doesn't find any audio files on their devices.
I use mediastore query and contentResolverto get the ID or path from the audio files.
I don't believe, that I have a problem in my code, so the question is, if mediastore or contentResolverdepends on devices?
Here is an example how I did searching for devices with Android lower Q:
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.DATE_ADDED
};
String sortOder = MediaStore.Audio.Media.TRACK + " ASC";
selection = MediaStore.Audio.Media.DATA + " like ?";
selectionArgs = new String[]{directory};
Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, sortOder);
Can someone give me a hint or has had a similar experience?
On development devices this behavior doesn't happen, so that no log is available.
I'm trying to select multiple folders by querying the mediaStore. I can select a single folder by sing this piece of code:
String[] whereVal;
String selection;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.DATA + " like ? " ;
whereVal = new String[]{"%Folder%"};
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
However when I try to select multiple folders mediaStore returns nothing.
Code:
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.DATA + " IN(?,?)" ;
whereVal = new String[]{"%Top 20 English%","%Audio%"};
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
Care to help me out? Thank in advance.
PS: I don't want to use OR operator.
I am trying to get list of mp3 files from a particular folder.
As of now, I am using contentResolver.query for getting the music from my phone.
Like this:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
ContentResolver contentResolver = context.getContentResolver();
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, selection,null,null);
I am looking for solution to scan only a particular folder. How do I do this using contentResolver?
Thanks!
Finally figured it out myself. Hope it will help others.
Instead of this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
I used this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%MyMusicFolder%"}, null);
i use
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor;
cursor = сontext.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection, null, null);
and get data from tracks
while (cursor.moveToNext()) {
Log.e(TAG, "Track =" + cursor.getString(3));
Track track = new Track();
track.setLocalId(cursor.getInt(0));
track.setArtist(cursor.getString(1));
track.setAlbum(cursor.getString(2));
track.setTitle(cursor.getString(3));
track.setPath(cursor.getString(4));
track.setFileName(cursor.getString(5));
track.setLength(cursor.getLong(6));
track.setYear(cursor.getInt(7));
track.setAlbumId(cursor.getLong(8));
track.setGenre(context.getString(R.string.unknown_genre));
tracks.add(track);
}
cursor.close();
In the first case, on my real device HTC One V, in Log i see:
Track =Eye of the tiger(OST Рокки Бальбоа),
the second, on Genymotion emulator for example, in Log:
Track =Eye of the tiger(OST ?????????? ??????????????)
Please tell me how to solve this problem?
In the below code, the getcontentresolver() is null, but works in the onCreate() of Service but not in a function in the service. Why? plz help me..thanks
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
cursor = getContentResolver()
.query(uri, cursor_cols, where, null, null);
Make sure that your USB connection to the device is in PC mode, and not USB Mass Storage
if it is in Mass Storage, it will return null