How to get songs from an album / artist in android using MediametadataRetriever?

First of all, I'm a beginner, and secondly, I know that there is a solution for "How to get songs from an album in Android?" but this is with the cursor and MediaStore, and I used MediametadataRetriever, and it’s completely different, I didn’t use hashmaps, that's all ..... so its a little complicated ... please, any solutions for this ??? without the use of hashmaps and mediastor

here is the code that I used to display all the songs and its performers ..........

import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.io.Serializable; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.MediaMetadataRetriever; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class FragmentSongs extends Fragment implements Serializable { AdapterView.AdapterContextMenuInfo info; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_song, container, false); ListView SngList = (ListView) view.findViewById(R.id.SongList); registerForContextMenu(SngList); File f=new File("/sdcard/Music"); int j=0;int i=0; final ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f); if (Songinfo.size()>0) { for( j=0; j<Songinfo.size();j++) { for ( i=j+1 ; i<Songinfo.size(); i++) { SongDetails a=Songinfo.get(i); SongDetails b=Songinfo.get(j); if (a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase()) < 0) { Songinfo.set(i,b ); Songinfo.set(j,a); } } } SngList.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView a, View v, int position, long id) { Intent intent = new Intent(getActivity(), NowPlaying.class); intent.putParcelableArrayListExtra("Data1",Songinfo); intent.putExtra("Data2",position); startActivity(intent); } }); SngList.setAdapter(new CustomAdapter(Songinfo)); return view; } else { return null; } } public ArrayList<SongDetails> getSongsFromDirectory(File f) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); ArrayList<SongDetails> songs = new ArrayList<SongDetails>(); Bitmap bitmap2; Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ab); float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()); float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()); Bitmap bitmap3 = Bitmap.createScaledBitmap(bmp, (int) ht_px, (int) wt_px, true); byte[] rawArt = null; Bitmap art; BitmapFactory.Options bfo=new BitmapFactory.Options(); if (!f.exists() || !f.isDirectory()) { return songs; } File[] files = f.listFiles(new Mp3Filter()); for(int i=0; i<files.length; i++) { if (files[i].isFile()) { mmr.setDataSource(files[i].getPath()); rawArt = mmr.getEmbeddedPicture(); SongDetails detail=new SongDetails(); if ( rawArt != null) { bitmap2=BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo); bitmap2 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true); detail.setIcon(bitmap2); } else { detail.setIcon(bitmap3); } detail.setSong(files[i].getName()); detail.setArtist(files[i].getName()); detail.setArtist( mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); detail.setAlbum( mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); detail.setPath2( files[i].getPath()) ; songs.add(detail); } else if (files[i].isDirectory()) { songs.addAll(getSongsFromDirectory(files[i])); } } return songs; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); info = (AdapterContextMenuInfo) menuInfo; menu.add(Menu.NONE, v.getId(), 0, "Play"); menu.add(Menu.NONE, v.getId(), 0, "Delete"); menu.add(Menu.NONE, v.getId(), 0, "Queue Item"); } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getTitle() == "Play") { } else if (item.getTitle() == "Delete") { } else if (item.getTitle() == "Queue Item") { } else { return false; } return true; } } class Mp3Filter implements FileFilter { public boolean accept(File file) { return (file.isDirectory()||file.getName().endsWith(".mp3")|| file.getName().endsWith(".Mp3")); } } 
+7
android media
source share
2 answers

Check out this tutorial:

http://www.srikanthtechnologies.com/blog/android/audioplayer.aspx

You can change it a little according to your needs:

 private ArrayList<SongDeatils> getSongList(File musicFolder) { ArrayList<SongDeatils> songs = new ArrayList<SongDeatils>(); for (File f : musicFolder.listFiles()) { MediaMetadataRetriever md = new MediaMetadataRetriever(); md.setDataSource(musicFolder + "/" + f.getName()); // Assign variables like title, artist, etc SongDeatils s = new SongDeatils(); s.setTitle(title); s.setArtist(artist); s.setSong(f.getPath()); // ... songs.add(s); } return songs; } 

To process the image (cover), this really should be done asynchronously in the adapter. I would suggest using the Universal Image Downloader .

Update:

Just saw your comments. What you really want is an effective way to list the songs of a particular album or artist without using file filters and sorting. Another option is to use MediaStore / Cursor . There are various types of queries that you can create, depending on how you try to view the data.

So, to query the artist list, you must use MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI and use the artist name as your selection arguments.

For the album list, you now have ARTIST_KEY , which you can use to query MediaStore.Audio.Artists.Albums.EXTERNAL_CONTENT_URI to get the album list for this artist.

An example of how to list all albums for a given artist, see here .

+7
source share

@Anknit Mediastore does not detect all directories on your device. I do not scan much faster than what we are trying to create our own code. But he does not detect any folder. Today I create my project on my phone. I have directories, namely .trash-100 and .trash . It contains almost .mp3 files. It was not detected even by VLC and the default music player except Poweramp. Please refer to the following link http://mrbool.com/how-to-extract-meta-data-from-media-file-in-android/28130 I just used recursion for this and it worked great

0
source share

All Articles