Libin
Libin

Reputation: 17085

Android: How to get audio detail from audio file

I have my music app started from external applications using intent data as music file.

So I have the mp3 audio URI something like this

file:///storage/emulated/0/Music/Tamil/I%20(2014)/Ennodu%20Nee%20Irundhaal.mp3

how to get the audio details from URI ie, the Media.TITLE , Media.ALBUM , Media._ID

Upvotes: 7

Views: 14446

Answers (4)

Abdelemjid Essaid
Abdelemjid Essaid

Reputation: 51

public ScopedSong getSongDetails(String path) {
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
    String[] projection = new String[] {
            "title",
            "_id",
            "album",
            "album_id",
            "artist",
            "artist_id",
            "duration",
            "track"
    };
    @SuppressLint("Recycle")
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
    ScopedSong song = null;
    if (cursor != null && cursor.moveToFirst()) {
        song = new ScopedSong(cursor.getString(0), cursor.getLong(1), cursor.getString(2), cursor.getLong(3),
                cursor.getString(4), cursor.getLong(5), cursor.getInt(6), cursor.getInt(7));
        cursor.close();
    }
    return song;
}


public class ScopedSong {
private final String title, albumName, artistName;
private final long id, albumId, artistId;
private final int duration, trackNumber;

public ScopedSong(String title, long id, String albumName, long albumId, String artistName, long artistId, int duration, int trackNumber) {
    this.title = title;
    this.id = id;
    this.albumName = albumName;
    this.albumId = albumId;
    this.artistName = artistName;
    this.artistId = artistId;
    this.duration = duration;
    this.trackNumber = trackNumber;
}

public ScopedSong() {
    this.title = "";
    this.albumName = "";
    this.artistName = "";
    this.id = -1;
    this.albumId = -1;
    this.artistId = -1;
    this.duration = -1;
    this.trackNumber = -1;
}

public String getTitle() {
    return title;
}

public String getAlbumName() {
    return albumName;
}

public String getArtistName() {
    return artistName;
}

public long getId() {
    return id;
}

public long getAlbumId() {
    return albumId;
}

public long getArtistId() {
    return artistId;
}

public int getDuration() {
    return duration;
}

public int getTrackNumber() {
    return trackNumber;
}

}

Upvotes: 0

Sachin Mandhare
Sachin Mandhare

Reputation: 720

MediaMetaDataRetriever class: - MediaMetaDataRetriever class in android have many advantageous features to work on audio files. Its package name is “android.media.MediaMetadataRetriever” It is able to give predefined information of such files like:

  • Artist of song
  • Album name of song
  • Album art of song
  • Genre of song
  • Composer of song and many more options it has.

    MediaMetadataRetriever metaRetriver;
    metaRetriver = new MediaMetadataRetriever();
    metaRetriver.setDataSource("/sdcard/audio.mp3");
    

    The above code represent how to create object of MediaMetadataRetriever class and how to set the data source.

As in this code absolute path of audio file which is set of file which is in sd-card.

byte[] art;
art = metaRetriver.getEmbeddedPicture();

The above code is used to get album art in byte format from audio file.

Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);

The above code is used to convert metadata in byte form to Bitmap format so it can be easy to set on ImageView that is defined to show it.

All Code

ImageView album_art;
TextView album, artist, genre;

MediaMetadataRetriever metaRetriver;
byte[] art;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getInit();

    // Ablum_art retrieval code //

    metaRetriver = new MediaMetadataRetriever();
    metaRetriver.setDataSource("/sdcard/audio.mp3");
    try {
        art = metaRetriver.getEmbeddedPicture();
        Bitmap songImage = BitmapFactory
                .decodeByteArray(art, 0, art.length);
        album_art.setImageBitmap(songImage);
        album.setText(metaRetriver
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
        artist.setText(metaRetriver
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
        genre.setText(metaRetriver
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
    } catch (Exception e) {
        album_art.setBackgroundColor(Color.GRAY);
        album.setText("Unknown Album");
        artist.setText("Unknown Artist");
        genre.setText("Unknown Genre");
    }

}

// Fetch Id's form xml 

public void getInit() {

    album_art = (ImageView) findViewById(R.id.album_art);
    album = (TextView) findViewById(R.id.Album);
    artist = (TextView) findViewById(R.id.artist_name);
    genre = (TextView) findViewById(R.id.genre);

}

main.xml

<? xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/album_art_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="60dp"
    android:text="Album Art"
    android:textSize="18dp" />

<ImageView
    android:id="@+id/album_art"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_marginRight="10dp" />

<TextView
    android:id="@+id/album_name_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/album_art_text"
    android:layout_below="@+id/album_art"
    android:layout_marginTop="24dp"
    android:text="Album Name :"
    android:textSize="18dp" />

<TextView
    android:id="@+id/artist_name_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/album_name_text"
    android:layout_below="@+id/album_name_text"
    android:layout_marginTop="43dp"
    android:text="Artist Name :"
    android:textSize="18dp" />

<TextView
    android:id="@+id/genre_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/artist_name_text"
    android:layout_below="@+id/artist_name_text"
    android:layout_marginTop="39dp"
    android:text="Genre :"
    android:textSize="18dp" />

<TextView
    android:id="@+id/genre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/genre_text"
    android:layout_alignBottom="@+id/genre_text"
    android:layout_toRightOf="@+id/album_art_text"
    android:text="null"
    android:textSize="18dp" />


<TextView
    android:id="@+id/Album"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/artist_name_text"
    android:layout_alignLeft="@+id/album_art"
    android:text="null"
    android:textSize="18dp" />

<TextView
    android:id="@+id/artist_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/genre_text"
    android:layout_alignLeft="@+id/Album"
    android:text="null"
    android:textSize="18dp" />

</RelativeLayout>

Upvotes: 12

Ahmad Azarnia
Ahmad Azarnia

Reputation: 127

You can use this code for get details of audio file:

String path = file:///storage/emulated/0/Music/Tamil/I%20(2014)/Ennodu%20Nee%20Irundhaal.mp3
String where = String.format("%s='%s'", MediaStore.Audio.Media.DATA, path);
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor cursor = mContext.getContentResolver().query(uri, audioProjection, where, null, null);

    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        mAudioFile.setId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
        mAudioFile.setData(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
        mAudioFile.setDisplay_name(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
        mAudioFile.setSize(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
        mAudioFile.setMime_type(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE)));
        mAudioFile.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
        mAudioFile.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
        mAudioFile.setArtist_id(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST_ID)));
        mAudioFile.setComposer(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.COMPOSER)));
        mAudioFile.setAlbum_id(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)));
        mAudioFile.setTrack(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TRACK)));
        mAudioFile.setYear(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR)));
        mAudioFile.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
        mAudioFile.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
    }

Upvotes: 3

gilchris
gilchris

Reputation: 1231

You can converto file URI to canonical path and get infomation of music file with ContentsProvider like below codes.

String path = new File(new URI(path).getPath()).getCanonicalPath();
Cursor c = context.getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    new String[] {
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TRACK,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.YEAR
    },
    MediaStore.Audio.Media.DATA + " = ?",
    new String[] {
            path
    },
    "");

if (null == c) {
    // ERROR
}

while (c.moveToNext()) {
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.TRACK));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
    c.getString(c.getColumnIndex(MediaStore.Audio.Media.YEAR));
}

Upvotes: 6

Related Questions