您的位置:首页 > 其它

MediaMetadataRetriever的用法

2015-08-27 18:41 344 查看
API说明:MediaMetadataRetriever class provides a unified interface for retrieving frame and meta data from
an input media file.

MediaMetadataRetriever类提供了一个统一的接口用于从一个输入媒体文件中取得帧和元数据。
API官方链接:http://developer.android.com/reference/android/media/MediaMetadataRetriever.html

从本地视频文件获取视频缩略图:

/**
* 获取视频的缩略图
* 提供了一个统一的接口用于从一个输入媒体文件中取得帧和元数据。
* @param path 视频的路径
* @param width 缩略图的宽
* @param height 缩略图的高
* @return 缩略图
*/
public static Bitmap createVideoThumbnail(String path, int width, int height) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
if (TextUtils.isEmpty(path)) {
return null;
}

File file = new File(path);
if (!file.exists()) {
return null;
}

try {
retriever.setDataSource(path);
bitmap = retriever.getFrameAtTime(-1); //取得指定时间的Bitmap,即可以实现抓图(缩略图)功能
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}

if (bitmap == null) {
return null;
}

bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
return bitmap;
}
从一个音乐文件中取得部分媒体信息:

public class MainActivity extends Activity {
private static final String TAG = "Jackie";

@TargetApi(10)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
String path = getExternalStorageDirectory() + "music/hetangyuese.mp3";
Log.d(TAG, "path: " + path);
try {
mmr.setDataSource(path);
String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); // API LEVEL 10, 即从GB2.3.3开始有此功能
Log.d(TAG, "title:" + title);
String album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
Log.d(TAG, "album:" + album);
String mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
Log.d(TAG, "mime:" + mime);
String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
Log.d(TAG, "artist:" + artist);
String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); // 播放时长单位为毫秒
Log.d(TAG, "duration:" + duration);
String bitrate = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); // 从api level 14才有,即从ICS4.0才有此功能
Log.d(TAG, "bitrate:" +bitrate);
String date = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
Log.d(TAG, "date:" + date);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public static String getExternalStorageDirectory()
{
return Environment.getExternalStorageDirectory() + "/";
}
}


信息输出如下:

08-09 15:53:52.119: D/MainActivity(5293): str:/mnt/sdcard/music/hetangyuese.mp3

08-09 15:53:52.810: D/MainActivity(5293): title:荷塘月色

08-09 15:53:52.829: D/MainActivity(5293): album:我从草原来

08-09 15:53:52.859: D/MainActivity(5293): mime:audio/mpeg

08-09 15:53:52.868: D/MainActivity(5293): artist:凤凰传奇

08-09 15:53:52.893: D/MainActivity(5293): duration:9172

08-09 15:53:52.899: D/MainActivity(5293): bitrate:128000

08-09 15:53:52.909: D/MainActivity(5293): date:null
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: