Android:如何从音频文件中获取音频细节

时间:2015-10-27 04:37:57

标签: android android-music-player

我的音乐应用程序是使用意图数据作为音乐文件从外部应用程序启动的。

所以我有像这样的mp3音频URI

  

文件:///存储/模拟/ 0 /音乐/泰米尔/ I%20(2014)/Ennodu%20Nee%20Irundhaal.mp3

如何从URI获取音频详细信息,即Media.TITLE,Media.ALBUM,Media._ID

3 个答案:

答案 0 :(得分:7)

MediaMetaDataRetriever类: - android中的MediaMetaDataRetriever类具有许多有利于音频文件的功能。它的包名是“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);

上面的代码用于将字节格式的元数据转换为位图格式,因此可以很容易地在定义显示它的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'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>

答案 1 :(得分:5)

您可以将文件URI转换为规范路径,并使用ContentsProvider获取音乐文件的信息,如下面的代码。

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

答案 2 :(得分:2)

您可以使用此代码获取音频文件的详细信息:

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