本文主要是介绍MediaMetadataRetriever的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
这篇关于MediaMetadataRetriever的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!