生活随笔
收集整理的這篇文章主要介紹了
android 获取MP4文件的图片大小
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天,簡單講講怎么獲取MP4視頻的每一幀的圖片大小。
android使用?MediaMetadataRetriever 獲取視頻文件的 總時長 和視頻的分辨率。
根據該方式獲取視頻信息可以看出不僅僅可以獲取時長和分辨率,還能獲取到其他的一些視頻信息,不錯還是很強大的,不用自己去解析mp4文件了。此demo只是獲取的mp4文件,其他的多媒體文件也可以獲取到相應信息,比如mp3;
private void getPlayTime(String mUri){android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();try {if (mUri != null) {HashMap<String, String> headers = null;if (headers == null) {headers = new HashMap<String, String>();headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");}mmr.setDataSource(mUri, headers); //獲取網絡MP4視頻圖片的大小} else {//mmr.setDataSource(mFD, mOffset, mLength);}String duration = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);//時長(毫秒)String width = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);//寬String height = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);//高Toast.makeText(MainActivity.this, "playtime:"+ duration+"w="+width+"h="+height, Toast.LENGTH_SHORT).show();} catch (Exception ex) {Log.e("TAG", "MediaMetadataRetriever exception " + ex);} finally {mmr.release();}}
這里面的代碼是通過MediaMetadataRetriever獲取網絡視頻的圖片大小,其實獲取手機本地的MP4視頻的圖片大小也可以,不過在小米手機獲取本地視頻大小時會出現權限禁止,導致獲取失敗。所以獲取本地視頻時需要修改代碼。
//根據url獲取音視頻時長,返回毫秒public long getDurationLong(String url,int type){String duration = null;MediaMetadataRetriever retriever = new MediaMetadataRetriever();try {//如果是網絡路徑if(type == NETWORK){retriever.setDataSource(url,new HashMap<String, String>());}else if(type == LOCAL){//如果是本地路徑retriever.setDataSource(url);}duration = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);} catch (Exception ex) {LogUtils.e(ex);LogUtils.d("nihao", "獲取音頻時長失敗");} finally {try {retriever.release();} catch (RuntimeException ex) {LogUtils.e(ex);LogUtils.d("nihao", "釋放MediaMetadataRetriever資源失敗");}}if(!TextUtils.isEmpty(duration)){return Long.parseLong(duration);}else{return 0;}}//獲取視頻縮略圖private Bitmap createVideoThumbnail(String url, int type) {Bitmap bitmap = null;MediaMetadataRetriever retriever = new MediaMetadataRetriever();try {//將網絡文件以及本地文件區分開來設置if (type == NETWORK) {retriever.setDataSource(url, new HashMap<String, String>());} else if(type == LOCAL){retriever.setDataSource(url);}bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_NEXT_SYNC);} catch (IllegalArgumentException ex) {LogUtils.e(ex);LogUtils.d("nihao", "獲取視頻縮略圖失敗");} finally {try {retriever.release();} catch (RuntimeException ex) {LogUtils.e(ex);LogUtils.d("nihao", "釋放MediaMetadataRetriever資源失敗");}}return bitmap;}
這個是獲取視頻的時長和縮略圖,里面有判斷本地和網絡視頻的區別,
???
????????????if(type?==?NETWORK){??
????????????????retriever.setDataSource(url,new?HashMap<String,?String>());??
????????????}else?if(type?==?LOCAL){??
????????????????retriever.setDataSource(url);??
????????????}??
只是把setDataSource()的代碼修改了,其他沒有變化。
其實重點在于兩個方法都對于傳入的url進行了是否是網絡的判斷,那么接下來我們看看傳入不同參數的setDataSource有什么不同吧,官方解釋如下:
這個是我用于獲取本地文件信息的方法,可以看下我標紅的地方,path的定義是你傳入的媒體問題的路徑,所以就應該是對應的本地文件路徑了。但是如果路徑無效是會拋出異常的。
這個是我用于獲取網絡視頻信息的方法,也可以看下我標紅的部分,這個方法是從Android 4.0才開始有的。另外那個第二個參數是請求數據時候攜帶的頭信息。但是我這里沒有做特殊的設置了。就傳遞了一個map對象;
android 獲取MP4文件的圖片大小就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android 获取MP4文件的图片大小的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。