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



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值