Android: how to get audio data from an audio file

I have a music application that runs from external applications using intent data as a music file.

So I have an mp3 audio URI something like this

File: /// storage / emulate / 0 / Music / Tamil / I% 20 โ€‹โ€‹(2014) /Ennodu%20Nee%20Irundhaal.mp3

how to get audio data from a URI, i.e. Media.TITLE, Media.ALBUM, Media._ID

+4
source share
3 answers

You can convert the file URI to the canonical path and retrieve information about the music file using the ContentProvider, as shown below.

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));
}
+3
source

MediaMetaDataRetriever: - MediaMetaDataRetriever android . - "android.media.MediaMetadataRetriever". , :

  • .

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

    MediaMetadataRetriever .

, sd-card.

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

.

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

Bitmap, ImageView, .

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 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>
+6

:

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)));
    }
+2

Source: https://habr.com/ru/post/1613285/


All Articles