[英]Android: How to get audio detail from audio file
我的音乐应用程序从外部应用程序启动,使用意图数据作为音乐文件。
所以我有这样的 mp3 音频 URI
file:///storage/emulated/0/Music/Tamil/I%20(2014)/Ennodu%20Nee%20Irundhaal.mp3
如何从 URI 获取音频详细信息,即 Media.TITLE、Media.ALBUM、Media._ID
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>
您可以将文件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));
}
您可以使用此代码获取音频文件的详细信息:
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)));
}
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.