2

I am new in android. I am working on app based on Media Player. My issue is how to get the artist , song duration , album etc for the song which is playing. I have seen the android.media.RemoteControlClient.MetadataEditor class but is included from Api 14. If you have any idea Pls suggest

Thanks in advance.

Rajendra
  • 1,700
  • 14
  • 17

3 Answers3

1

You need to use the MediaMetadataRetriever.

Example:

myUri = Uri.parse("android.resource://your.package.name/" + R.raw.yourSoundFile);
retriever = new MediaMetadataRetriever();
retriever.setDataSource(this, myUri);
album = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
Toast.makeText(this,album , Toast.LENGTH_SHORT).show();

Other fields can be found in javadoc, such as:

  • METADATA_KEY_ALBUMARTIST
  • METADATA_KEY_ARTIST
  • METADATA_KEY_AUTHOR
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
1

Study the following

http://developer.android.com/reference/android/media/MediaPlayer.html http://developer.android.com/reference/android/provider/MediaStore.Audio.html

in the second one Audio class contains classes like Album, Artist etc. By using them you can find the album artist etc.

Anil
  • 153
  • 1
  • 6
1

For this you can use the content provider to query media store and do any kind of modifications to the tables from there.

A complete nice Example:

//Some audio may be explicitly marked as not being music
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
};

cursor = this.managedQuery(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    projection,
    selection,
    null,
    null);

private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()){
    songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5));
}
// see http://androidsnippets.com/list-all-music-files
httpSteve
  • 25
  • 8
user370305
  • 108,599
  • 23
  • 164
  • 151
  • THANKS to reply,But if i am playing the song through streaming – Rajendra Jan 27 '12 at 17:56
  • @Ankit - `managedQuery` is the method of Activity Class which manged cursor as per activity life cycle. – user370305 Aug 04 '13 at 07:38
  • i can't get it in a fragment –  Aug 04 '13 at 10:02
  • 1
    @Ankit - Its only for Activity Class. So you can access it in FragmentActivity or in Fragment using `getActivity().managedQuery();` – user370305 Aug 04 '13 at 18:43
  • Well thanx for the information,but i used mediametadata instead...can you reply one more simple question???its on simple sorting? http://stackoverflow.com/questions/12986386/sorting-an-array-of-strings-with-java –  Aug 04 '13 at 18:45
  • my question is why is the code that has been provided there,not working? –  Aug 04 '13 at 18:46