MediaMetaDataRetriever 类: - Android 中的 MediaMetaDataRetriever 类具有许多处理音频文件的有利功能。它的包名称是“android.media.MediaMetadataRetriever”它能够提供此类文件的预定义信息,例如:
如这段代码所示,音频文件的绝对路径是 SD 卡中的文件集。
byte[] art;
art = metaRetriver.getEmbeddedPicture();
上面的代码用于从音频文件中获取字节格式的专辑封面。
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
上面的代码用于将字节形式的元数据转换为位图格式,以便可以轻松地在定义的 ImageView 上进行设置以显示它。
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>