//this method returns me name of song.
private void getMusic() {
String[] STAR = { "*"};
Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = getContentResolver().query(uri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int i = 0;
do {
//this returns name of the song
String songName = cursor.getString(cursor.
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
//this returns path of the song
path[i] = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
//this returns album name of the song
String albumName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
//this returns albumid of the song
int albumId = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
//this will add song in listview
song.add(songName);
i++;
} while (cursor.moveToNext());
b = i;
}
cursor.close();
}
Related
I'm trying to list all the albums of songs available in my sd card. I did it successfully but I have a small problem. Example, if I an album "ABC", which contains 10 artists. Then, when I query for albums, it shows album "ABC" 10 times based on the artist. Album "ABC" will be displayed 10 times and each album "ABC" will contain songs of the particular artist.
It is displayed like this,
ABC\artistA
ABC\artistB........so on
What I want is it should display the album "ABC" once and when I open it all the songs of that album should be there.
Code:
final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
final String _id = MediaStore.Audio.Albums._ID;
final String album_name = MediaStore.Audio.Albums.ALBUM;
final String totSongs = MediaStore.Audio.Albums.NUMBER_OF_SONGS;
final String artist_Name = MediaStore.Audio.Albums.ARTIST;
final String[] columns = { _id, album_name, totSongs,artist_Name };
Cursor cursor = getActivity().getContentResolver().query(uri, columns, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
id = cursor.getLong(cursor.getColumnIndex(_id));
String name = cursor.getString(cursor.getColumnIndex(album_name));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
albumArtUri = ContentUris.withAppendedId(sArtworkUri, id).toString();
totalSongs = cursor.getString(cursor.getColumnIndex(totSongs));
ArtistName = cursor.getString(cursor.getColumnIndex(artist_Name));
AlbumsModel s = new AlbumsModel(id, name, albumArtUri,totalSongs,ArtistName);
AlbumsList.add(s);
} while (cursor.moveToNext());
}
cursor.close();
Instead of using a List, you could use Map:
HashMap<String, AlbumModel> albums = new HashMap();
Cursor cursor = getActivity().getContentResolver().query(uri, columns, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
id = cursor.getLong(cursor.getColumnIndex(_id));
String name = cursor.getString(cursor.getColumnIndex(album_name));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
albumArtUri = ContentUris.withAppendedId(sArtworkUri, id).toString();
totalSongs = cursor.getString(cursor.getColumnIndex(totSongs));
ArtistName = cursor.getString(cursor.getColumnIndex(artist_Name));
AlbumsModel album = albums.get(id);
if(album == null){
album = new AlbumsModel(id, name, albumArtUri,totalSongs,ArtistName);
albums.put(id, album);
continue;
}
//update album music count
album.setTotalMusic(album.getTotalMusic() + totSongs);
albums.put(id, album);
} while (cursor.moveToNext());
}
cursor.close();
This is how you could display the Album/Artist with the Album only appearing once (this assumes that the cursor is sorted by Album) :-
String previous_album = "";
for (AlbumsModel am: AlbumsList) {
if(am.getAlbumName().equals(previous_album)) {
Log.d("ALBUMINFO","\t\t" + am.getArtistName());
} else {
Log.d("ALBUMINFO",am.getAlbumName() + "\t" + am.getArtistName());
}
previous_album = am.getAlbumName();
}
This would follow your code and write the output to the log (obviously you would tailor this to suit) e.g. :-
01-24 20:54:21.705 2125-2125/albums.so.so48422731albums D/ALBUMINFO: ABC ArtistA
01-24 20:54:21.705 2125-2125/albums.so.so48422731albums D/ALBUMINFO: ArtistB
01-24 20:54:21.705 2125-2125/albums.so.so48422731albums D/ALBUMINFO: ArtistC
i want to make a music player. but i couldn't get all the .mp3 files from internal and external storage. can anyone help me please.
Thanks in advance.
here is my code.
public void getListOfSong1(Context context) {
SongData.cart.clear();
Cursor c = context.getContentResolver().query(Media.INTERNAL_CONTENT_URI, null, "is_music != 0", null, null);
c.moveToFirst();
while (c.moveToNext()) {
SongData songData = new SongData();
int _id = c.getColumnIndex(MostAndRecentSongTableHelper.ID);
String title = c.getString(c.getColumnIndex(MostAndRecentSongTableHelper.TITLE));
String artist = c.getString(c.getColumnIndex(MostAndRecentSongTableHelper.ARTIST));
String album = c.getString(c.getColumnIndex(FavoriteSongsTableHelper.ALBUM));
long duration = c.getLong(c.getColumnIndex(MostAndRecentSongTableHelper.DURATION));
String data = c.getString(c.getColumnIndex("_data"));
long albumId = c.getLong(c.getColumnIndex(MostAndRecentSongTableHelper.ALBUM_ID));
String composer = c.getString(c.getColumnIndex("composer"));
songData.setId(_id);
songData.setSongTitle(title);
songData.setAlbum(album);
songData.setArtist(artist);
songData.setDuration(duration);
songData.setSongPath(data);
songData.setAlbumId(albumId);
if (!data.endsWith(".mp3")) {
if (!data.endsWith(".MP3")) {
}
}
this.listOfSongs.add(songData);
}
c.close();
}
Please find the function below which is able to get the all mp3 songs which you want.
public void getMp3Songs() {
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = managedQuery(allsongsuri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
fullsongpath.add(fullpath);
album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
db.closeDatabase();
}
}
fun getAllAudioFromDevice(context: Context): List<AudioModel>? {
val tempAudioList: MutableList<AudioModel> = ArrayList()
val uri: Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf<String>(
MediaStore.Audio.AudioColumns.DATA,
MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.ArtistColumns.ARTIST,
)
val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
/*val projection = arrayOf(
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
)*/
val c: Cursor? = context.contentResolver.query(
uri, projection, selection, null, null
)
if (c != null) {
while (c.moveToNext()) {
val audioModel = AudioModel()
val path = c.getString(0)
val name = c.getString(1)
val artist = c.getString(2)
audioModel.trackName = name
audioModel.trackDetail = artist
audioModel.trackUrl = path
if(path != null && (path.endsWith(".aac")
|| path.endsWith(".mp3")
|| path.endsWith(".wav")
|| path.endsWith(".ogg")
|| path.endsWith(".ac3")
|| path.endsWith(".m4a"))) {
Log.v(mTAG, "trackName :${audioModel.trackName}, trackDetail :${audioModel.trackDetail}, trackUrl :${audioModel.trackUrl}")
tempAudioList.add(audioModel)
}
}
c.close()
}
return tempAudioList
}
This is my code that gets list of songs:
List<String> getsonglist() {
List<String> songlist = new ArrayList<>();
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
// query failed, handle error.
} else if (!cursor.moveToFirst()) {
// no media on the device
} else {
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
do {
long thisId = cursor.getLong(idColumn);
String thisTitle = cursor.getString(titleColumn);
// ...process entry...
songlist.add(thisTitle);
} while (cursor.moveToNext());
}
return songlist;
}
I want to play that song by giving its path to music player() method.
You can get full path like this:
String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
So, Your function would be:
List<String> getsonglist() {
List<String> songlist = new ArrayList<>();
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
// query failed, handle error.
} else if (!cursor.moveToFirst()) {
// no media on the device
} else {
do {
String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
// ...process entry...
Log.e("Full Path : ", fullPath);
songlist.add(fullPath);
} while (cursor.moveToNext());
}
return songlist;
}
Try this one
String fullPath = cursor.getString(cursor.getColumnIndex("_data"));
I want to get songs and other MEDIA details from Album Id. All I have is Album Id and I tried many solutions but none of them succeed.
My code snippet:
ContentResolver contentResolver = getContentResolver();
Uri mediaUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, albumId);
Log.wtf("SKJDBKJ", mediaUri.toString());
Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);
// if the cursor is null.
if(mediaCursor != null && mediaCursor.moveToFirst())
{
Log.wtf("DSJK", "entered cursor");
//get Columns
int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
// Store the title, id and artist name in Song Array list.
do
{
long thisId = mediaCursor.getLong(idColumn);
String thisTitle = mediaCursor.getString(titleColumn);
String thisArtist = mediaCursor.getString(artistColumn);
Log.wtf("Title", thisTitle);
// Add the info to our array.
songArrayList.add(new Song(thisId, thisTitle, thisArtist));
}
while (mediaCursor.moveToNext());
// For best practices, close the cursor after use.
mediaCursor.close();
}
Log for mediaUri returns path to current album, e.g. : content://media/external/audio/media/41.
Someone tell me how do I do it?
I have done it by myself.You can write simple sqlite query for 'selection' string on contentResolver.query .This example can show you how to get songs by album Id. I think this is best way.
ArrayList<Song> songs = new ArrayList<>();
String selection = "is_music != 0";
if (albumId > 0) {
selection = selection + " and album_id = " + albumId;
}
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_ID
};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";
Cursor cursor = null;
try {
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor = getActivity().getContentResolver().query(uri, projection, selection, null, sortOrder);
if (cursor != null) {
cursor.moveToFirst();
int position = 1;
while (!cursor.isAfterLast()) {
Song song = new Song();
song.setTitle(cursor.getString(0));
song.setDuration(cursor.getLong(4));
song.setArtist(cursor.getString(1));
song.setPath(cursor.getString(2));
song.setPosition(position);
song.setAlbumId(cursor.getLong(6));
cursor.moveToNext();
}
}
} catch (Exception e) {
Log.e("Media", e.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
I have figured it out myself after a LOT of trial and errors. I don't know if it's the best and safest way to do so, but as far as it's working I am happy.
I changed my code a bit and compared Album IDs. Here's the snippet:
ContentResolver contentResolver = getContentResolver();
Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Log.wtf("SKJDBKJ", mediaUri.toString());
Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);
// if the cursor is null.
if(mediaCursor != null && mediaCursor.moveToFirst())
{
Log.wtf("DSJK", "entered cursor");
//get Columns
int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int albumId = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
// Store the title, id and artist name in Song Array list.
do
{
long thisId = mediaCursor.getLong(idColumn);
long thisalbumId = mediaCursor.getLong(albumId);
String thisTitle = mediaCursor.getString(titleColumn);
String thisArtist = mediaCursor.getString(artistColumn);
// Add the info to our array.
if(this.albumId == thisalbumId)
{
Log.wtf("SAME2SAME", String.valueOf(thisalbumId));
Log.wtf("SAME2SAME", String.valueOf(this.albumId));
songArrayList.add(new Song(thisId, thisTitle, thisArtist));
}
}
while (mediaCursor.moveToNext());
// For best practices, close the cursor after use.
mediaCursor.close();
}
I changed:
Albums to Media in MediaStore.Audio.Audio.xxx.
Got the album Id of Media.
Compared that to album Id I receive from bundle extras.
Only add those songs in the arrayList.
I guess this'll be the way for Artists too.
when you put the cursor.movenext function in to the while loop, the first line of
MediaStore database will be passed without saving the data because the function move the cursor and then check so its better to make a value instead of putting the movenext function into the while.
try {
Projection = new String[]{
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM
, MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE};
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Projection, null, null, null);
cursor.moveToFirst();
for (int i = 0; i < 5; i++) {
columns[i] = cursor.getColumnIndex(Projection[i]);
}
Music tempMusic = new Music();
while (next) {
tempMusic.setName(cursor.getString(columns[0]));
tempMusic.setAlbum(cursor.getString(columns[1]));
tempMusic.setArtist(cursor.getString(columns[2]));
tempMusic.setUri(cursor.getString(columns[3]));
tempMusic.setSize(cursor.getDouble(columns[4]));
if(!cursor.moveToNext())
{
next=false;
}
musicss.add(tempMusic);
tempMusic = new Music();
}
public Bitmap getAlbumArt(Long albumId) {
Bitmap albumArt = null;
try {
final Uri AlbumArtUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(AlbumArtUri, albumId);
ParcelFileDescriptor pfd = GlobalSongList.getInstance().getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
albumArt = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return albumArt;
}
ALBUM_ID is a field in the MediaStore and you can query it in the same way you query for title,artist,etc.
public Long getAlbumId(String id)
{
Cursor musicCursor;
String[] mProjection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID};
String[] mArgs = {id};
musicCursor = musicResolver.query(musicUri, mProjection, MediaStore.Audio.Media._ID + " = ?", mArgs, null);
musicCursor.moveToFirst();
Long albumId=musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
return albumId;
}
This is how I did.
Works perfect.
String selection= MediaStore.Audio.Media.IS_MUSIC+"!=0";
String[] projection={
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Media.DURATION
};
Cursor cursor=getActivity().managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,"title asc");
List<String> data=new ArrayList<>();
List<String> songs=new ArrayList<>();
List<String> artists=new ArrayList<>();
List<String> albums=new ArrayList<>();
List<String> album_id=new ArrayList<>();
List<String> durations=new ArrayList<>();
cursor.moveToFirst();
while (cursor.moveToNext()){
if(cursor.getString(5).equals(mAlbumId)) {
data.add(cursor.getString(1));
songs.add(cursor.getString(2));
artists.add(cursor.getString(3));
albums.add(cursor.getString(4));
album_id.add(mAlbumId);
durations.add(cursor.getString(6));
}
}
The playlist names can be found by a query on MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI
and then look at the MediaStore.Audio.PlaylistsColumns.NAME column.
There is a data column too, MediaStore.Audio.PlaylistsColumns._DATA, but it is returning null.
The list of songs (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI/id) do not seem to show any playlist affiliation.
Here is a bit from my program:
The gist is that you need the id for the playlist to grab the songs.
Basically you can take my code and change the where statement to .NAME +" = '"+name+"'",
private Cursor getPlaylists(String playlistId){
Cursor cursor = null;
String[] projection1 = {
MediaStore.Audio.Playlists._ID,
MediaStore.Audio.Playlists.NAME
};
cursor = this.managedQuery(
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
projection1,
MediaStore.Audio.Playlists._ID+ " = "+playlistId+"",
null,
null);
startManagingCursor(cursor);
cursor.moveToFirst();
playlist_id = cursor.getString(0);
playlist_id2 = cursor.getLong(0);
if(playlist_id2 > 0){
String[] projection = {
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members._ID
};
cursor = null;
cursor = this.managedQuery(
MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id2 ),
projection,
MediaStore.Audio.Media.IS_MUSIC +" != 0 ",
null,
null);
}
cManager(cursor,2,1);
return cursor;
}
Here is a working way to get tracks from a play list. Basically it loops the cursor through all the of the playlist query, and each time it gets the id of a member (track) and using that id of the track we can get other data such as path, artist, duration, album etc.
ContentResolver contentResolver = getContentResolver();
Uri playListUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID); //playlistID is the _ID of the given playlist
MediaMetadataRetriever mr = new MediaMetadataRetriever();
Cursor cursor = contentResolver.query(playListUri, null, null, null, null);
if(cursor != null)
{
if(cursor.moveToNext()) {
do {
String track_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] trackProjection = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DATA};
String selection = MediaStore.Audio.Media._ID + "=?";
String[] selectionArgs = new String[]{"" + track_id};
Cursor mediaCursor = contentResolver.query(mediaContentUri, trackProjection, selection, selectionArgs, null);
if (mediaCursor != null) {
if (mediaCursor.getCount() >= 0) {
mediaCursor.moveToPosition(0);
String song_title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String song_artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String song_album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String song_path = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
}
}
} while (cursor.moveToNext());
}
}
this is the way to get all playlist from the device
public ArrayList<String> getPlayList() {
ArrayList<String> arrayList=new ArrayList<String>();
String[] proj = {"*"};
Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
// In the next line 'this' points to current Activity.
// If you want to use the same code in other java file then activity,
// then use an instance of any activity in place of 'this'.
Cursor playListCursor= activity.managedQuery(tempPlaylistURI, proj, null,null,null);
if(playListCursor == null){
System.out.println("Not having any Playlist on phone --------------");
return arrayList;//don't have list on phone
}
System.gc();
String playListName = null;
System.out.println(">>>>>>> CREATING AND DISPLAYING LIST OF ALL CREATED PLAYLIST <<<<<<");
for(int i = 0; i <playListCursor.getCount() ; i++)
{
playListCursor.moveToPosition(i);
playListName = playListCursor.getString(playListCursor.getColumnIndex("name"));
System.out.println("> " + i + " : " + playListName );
arrayList.add(playListName);
}
if(playListCursor != null)
playListCursor.close();
return arrayList;
}