Android: 如何从音频文件中获取音频细节
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
您可以将文件 URI 转换为规范路径,并使用 Content Provider 获取音乐文件的信息,如下面的代码。
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));
}
MediaMetaDataRetriever class: - android 中的 MediaMetaDataRetriever class 具有许多处理音频文件的有利功能。它的包名是“android.media.MediaMetadataRetriever”,它能够提供此类文件的预定义信息,例如:
- 歌手
- 歌曲专辑名称
- 专辑曲目
- 歌曲类型
歌曲的作曲者以及它拥有的更多选项。
MediaMetadataRetriever metaRetriver;
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource("/sdcard/audio.mp3");
以上代码表示如何创建MediaMetadataRetriever对象class以及如何设置数据源。
在这段代码中,音频文件的绝对路径是 sd 卡中的一组文件。
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'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>
您可以使用此代码获取音频文件的详细信息:
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;
}
}
我的音乐应用程序从外部应用程序启动,使用意图数据作为音乐文件。
所以我有类似这样的 mp3 音频 URI
file:///storage/emulated/0/Music/Tamil/I%20(2014)/Ennodu%20Nee%20Irundhaal.mp3
如何从 URI 中获取音频详细信息,即 Media.TITLE、Media.ALBUM、Media._ID
您可以将文件 URI 转换为规范路径,并使用 Content Provider 获取音乐文件的信息,如下面的代码。
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));
}
MediaMetaDataRetriever class: - android 中的 MediaMetaDataRetriever class 具有许多处理音频文件的有利功能。它的包名是“android.media.MediaMetadataRetriever”,它能够提供此类文件的预定义信息,例如:
- 歌手
- 歌曲专辑名称
- 专辑曲目
- 歌曲类型
歌曲的作曲者以及它拥有的更多选项。
MediaMetadataRetriever metaRetriver; metaRetriver = new MediaMetadataRetriever(); metaRetriver.setDataSource("/sdcard/audio.mp3");
以上代码表示如何创建MediaMetadataRetriever对象class以及如何设置数据源。
在这段代码中,音频文件的绝对路径是 sd 卡中的一组文件。
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'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>
您可以使用此代码获取音频文件的详细信息:
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;
}
}