Is there any way to fetch song's genre using MediaStore? - android

Using this method of audio file retrieval from Android's external storage
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
can I actually find a resonable way to fetch a genre of the given song? MediaStore class seems to provide everything else - from song's title to its composer info - except for the genre field. Should I use MediaMetadataRetriever then? If so, how drastically can creating a MediaMetadataRetriever instance for every song on a device reduce app's performance?
Maybe there are some better ways to retrieve all audio files from both external and internal storages in android?

As mentioned at Developer's Site,
You can fetch the Genres of the Audio file using MediaStore.Audio.Genres
Sample Code :
private static String[] genresProj = {
MediaStore.Audio.Genres.NAME,
MediaStore.Audio.Genres._ID
};
int idIndex = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
while (cursor.moveToNext()){
int id = Integer.parseInt(mediaCursor.getString(idIndex));
Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId("external", id );
genresCursor = context.getContentResolver().query(uri,
genresProj , null, null, null);
int genreIndex = genresCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);
while (genresCursor.moveToNext()) {
Log.d(TAG, "Genre = " +genresCursor.getString(genreIndex));
}
}
}
To fetch other details of the Audio file, please check here .

Related

avoid ringtone while getting audio files from mediastore

The way I currently get audio files on my devices is by iterating over a cursor like this:
val Cursor = applicationContext.contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
null);
Cursor?.let {
it.moveToFirst();
while(it.moveToNext()){
val title = it.getString(it.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
Log.d("SONGLIST: ", " Song title"+title);
}
}
It does get the audio files, but, includes audio files like system ringtone, alert audio files etc.
I tried doing a query like this:
val Cursor = applicationContext.contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
"${MediaStore.Audio.Media.IS_MUSIC} = 1",
null,
null);
Cursor?.let {
it.moveToFirst();
while(it.moveToNext()){
val title = it.getString(it.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
Log.d("SONGLIST: ", " Song title"+title);
}
}
but, using this query I get no results at all, the cursor.getCount becomes 0. So, how do I fetch all audio files in my device using android's MediaStore avoid system sound files?

MediaStore show files after deleted

I use MediaStore to get all video files from Android device. Then I delete some of these videos. Followed I use MediaStore again, and I get all deleted files.
Why MediaStore returns files that are no longer are on the device?
Delete File:
File file = new File(filePath);
file.delete();
Get all video files from device:
public static List<String> getVideoFiles(Context context) {
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null,
null, null);
List<String> videoList = new ArrayList<String>();
while (cursor.moveToNext()) {
videoList.add(cursor.getString(0));
}
Log.i(Constants.LOG_TAG, "get video files, load: " + videoList.size() + " "
+ videoList.toString());
return videoList;
}
MediaStore updates the list of media is not in real time. It needed time to test the relevance of its database. Try to make a call MediaStore after some time.
Or report manually about updating content.

Android ContentResolver.query always returns same data

I have videoplayer app with filebrowser listing all videos on SD card
Code inspired by i want get audio files in sd card
Using ContentResolver, works as expected, but it does not update if the files on card change. I do not mean automatically, but after view/app restart. Not even reinstalling the application helped, still shows the same files. The deleted video file is not visible via PC nor it is possible to play it (This video cannot be played (translation)).
I dumped the data and the problem is not in view caching or elsewhere. I do not implement any caching of my own and failed to find anything on the matter. Thank you
Code:
// acquisition
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATA
};
ContentResolver resolver = getActivity().getContentResolver();
Cursor videoCursor = resolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null
);
// extraction
while(cursor.moveToNext()) {
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
filepath = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
filename = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
duration = cursor.getString(cursorIndex);
result[ index++ ] = new VideoFileMetadata(filename, duration, filepath);
}
Edit 1 [14-03-2013]:
I tried adding number + " = " + number to ORDER or WHERE clause to act as a potential query caching buster, but it had no effect (although it's possible it was removed by an optimizer as a useless clause). This time I had reinstalled the application from a different machine using different certificate, but the query result remained the same, listing currently non-existing files.
You should first call cursor.moveToFirst() .
So, your cursor iteration loop should look like
if (cursor.moveToFirst()) {
do {
// cursorIndex = cursor.getColumnIndexOrThrow, etc...
} while (cursor.moveToNext());
}

Play a song with given Audio_id in android

I am trying to get a song from a playlist then play it in android.
All answers i can find relies on
MediaStore.MediaColumn.DATA
to find the file path and then feed it to the MediaPlayer.
But when i tried to do it, i kept getting "invalid column _data" exception.I can still query for other stuff about the song, like the "AUDIO_ID". So my question is, is it possible to play the song with only the "AUDIO_ID" known? How? Or is there something that i am missing for the "data", since every other people are able to use it.
This is my code for getting playlist.
private Cursor getPlaylistCursor() {
String[] proj = { MediaStore.Audio.Playlists._ID,MediaStore.Audio.Playlists.NAME };
Uri playlistUri = Uri.parse("content://com.google.android.music.MusicConten/playlists");
Cursor playlistCursor = getContentResolver().query(playlistUri, proj,null, null, null);
playlistCursor.moveToFirst();
return playlistCursor;
}
This is what i am working on for getting song, as i said, i cannot query the "data", if the "data" argument is added to the projection, i get an exception.
private void getSongListCursor(Long playlistID) {
String[] proj2 = { MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members.AUDIO_ID };
String playListRef = "content://com.google.android.music.MusicContent/playlists/"
+ playlistID + "/members";
Uri songUri = Uri.parse(playListRef);
Cursor songCursor = getContentResolver().query(songUri, proj2, null,
null, null);
}
SO now, i have the audio ID of a song, how do i play it?
Everything is possible ;-)
My approach is to use the Google Play Music server to obtain the streaming URL corresponding to the selected song ID. Just let me know if you need help with authentication and jump-start with the unofficial API for Google Play Music.

Android: MediaStore is missing artists and albums

A project I'm currently working on requires the application to discover all audio tracks on an android device. In addition to the tracks, it must also be able to group them by album and artist.
I found the MediaStore content provider and set about creating a database helper utility class to quickly return the IDs of tracks, albums and artists. I have been able to query the Media Store which returns some result, but it appears that not all of the audio information is stored there.
For example, querying for all artists returns only 9 results, but the Android Music Player application returns 27.
I am using the following code to query the artists:
ContentResolver resolver = getContentResolver();
String[] projection = new String[]{MediaStore.Audio.ArtistColumns.ARTIST};
Uri uri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor c = null;
try {
c = resolver.query(uri, projection, null, null, null);
}
catch (UnsupportedOperationException e) {
Log.e("DatabaseHelper", "query: " + e);
c = null;
}
if(c != null) {
while(c.isAfterLast() == false) {
Log.e("ARTIST", "NAME: " + cursor.getString(0));
cursor.moveToNext();
}
}
It seems as if the Media Scanner (which is definatley run when my device boots up) is not detecting much of my audio library. Am I doing something wrong?
I am simply trying to find all audio tracks, audio albums and audio artists quickly and efficiently. If MediaStore can't help me then I fear I will have to implement some form of file scanner to traverse directory structures and build my own database, but I don't want to do that ;)
Any thoughts would be much appreciated.
Thanks.
Probably, You should also query android.provider.MediaStore.Audio.Albums.INTERNAL_CONTENT_URI to get the rest visible in media player.

Categories

Resources