Album 获取参数
前言
提示:视频的时长怎么获取到?昨天有试过这种写法,但是不够细心没写完导致闪退,后面参考了this github!!
(2020/12/02)
一、获取视频时长步骤
1.AlbumBeanVideo中添加对象
private long mDuration;
//添加读取
protected AlbumBeanVideo(Parcel in) {
mDuration = in.readLong();
}
public void setDuration(long duration){
mDuration = duration;
}
public long getDuration(){
return mDuration;
}
//将对象写入Parcelable
public void writeToParcel(Parcel dest, int flags){
dest.writeLong(mDuration);
}
2.在FileUtils中添加时长的转换
这里是拿的是
这个github上的代码
/**
* Time conversion.
*
* @param duration ms.
* @return such as: {@code 00:00:00}, {@code 00:00}.
*/
@NonNull
public static String convertDuration(@IntRange(from = 1) long duration) {
duration /= 1000;
int hour = (int) (duration / 3600);
int minute = (int) ((duration - hour * 3600) / 60);
int second = (int) (duration - hour * 3600 - minute * 60);
String hourValue = "";
String minuteValue;
String secondValue;
if (hour > 0) {
if (hour >= 10) {
hourValue = Integer.toString(hour);
} else {
hourValue = "0" + hour;
}
hourValue += ":";
}
if (minute > 0) {
if (minute >= 10) {
minuteValue = Integer.toString(minute);
} else {
minuteValue = "0" + minute;
}
} else {
minuteValue = "00";
}
minuteValue += ":";
if (second > 0) {
if (second >= 10) {
secondValue = Integer.toString(second);
} else {
secondValue = "0" + second;
}
} else {
secondValue = "00";
}
return hourValue + minuteValue + secondValue;
}
3.在AlbumPreviewFragmentVideo中的onDialogInfo()方法添加
//内容对象
AlbumBeanVideo mb = mData.get(position);
//拿视频时长数据
MediaPlayer player = new MediaPlayer();
try{
//这样写其实有报一个错,因为这个setDaSource
player.setDataSource(mb.path); //这里
player.prepare();
mb.setDuration(player.getDuration());
//第二种写法
File file = new File(albumBean.path); //修改为这三句
FileInputStream fileInputStream = new FileInputStream(file);
player.setDataSource(fileInputStream.getFD());
player.prepare();
albumBean.setDuration(player.getDuration());
} catch (Exception e) {
e.printStackTrace();
}finally {
player.release();
}
//在AlterDialog中显示
infoDialog.setMessage(this.getResources().getString(R.string.info_name)+FileUtils.obtainFileName(mb.path) //名称
+"\n\n"+this.getResources().getString(R.string.info_local) + mb.getPath() //文件路径
+"\n\n"+this.getResources().getString(R.string.info_date) + FileUtils.parseDate(new File(mb.path)) //拍摄日期
+"\n\n"+"时长: " +FileUtils.convertDuration(mb.getDuration())
);
简单获取时长的方式!!
//如果是有显示时长文字框的话
if (mediaHolder.taAlbumView.iv_play.getVisibility() == View.VISIBLE){
//获取音视频文件信息
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//设置要使用的数据源(文件路径名)
retriever.setDataSource(albumBean.path);
//获取信息
String timeString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Long time = Long.valueOf(timeString);
//把获取的信息给到setDuration()
albumBean.setDuration(time);
//显示获取到的信息
mediaHolder.taAlbumView.setDuration(FileUtils.convertDuration(albumBean.getDuration()));
}
二、获取文件大小
1.去添加对象
2.在FileUtils中添加大小的转换
/**
* 文件大小
* @return*/
public static String FileSize(long mSize){
DecimalFormat sizedf = new DecimalFormat("#.## ");
String sizeString = "";
String wrongSize = "00.00B";
long KB = 1024;
long MB = KB*1024;
long GB = MB*1024;
if (mSize == 0){
//错误输出显示00.00B
return wrongSize;
}
if (mSize < KB){
Log.d(TAG, "VideoSize: "+mSize);
sizeString = sizedf.format((double) mSize) + "B";
}
else if (mSize<MB){
sizeString = sizedf.format((double) mSize/KB)+"KB ";
}
else if (mSize<GB){
sizeString = sizedf.format((double) mSize/MB)+"MB ";
}
else{
sizeString = sizedf.format((double) mSize/GB)+"GB ";
}
return sizeString;
}
3.去使用reference
目前有两种实现方法,一种是使用File的length()方法;另外一种是使用FileInputStream的available()方法,当InputStream未进行read操作时,available()的大小应该是等于文件大小的。但是在处理大文件时,后者会发生问题。(这段话出自那篇reference)
我们使用第一种方法,那么我们需要这个
File file = new File(mb.path);
使用显示出来
+"\n\n"+"文件大小: "+FileUtils.FileSize(file.length())
总结
一:
1、首先定义变量mDuration
2、这个变量是Long类型的,注意转换成你想要的格式converDuration
3、在MediaPlayer中去拿时长数据mb.setDuration(player.getDuration());这里注意释放资源
4、拿到给mb,再进行转换,显示。
二:
基本一致,就是获取文件的长度使用file.length()方法
三、获取视频帧率
//获取视频总帧数
String allFrame = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT);
long frame=Long.parseLong(allFrame);
//帧率
int fps = 0;
if(time > 0){
float millFps = frame * 1f / time; // 帧/秒
fps = (int)(millFps * 1000);
if (fps<40){ fps=30; }
else if (fps>40 && fps<70){ fps = 60;}
}
//不再需要MediaMetadataRetriever类获取音视频文件信息时记得释放
//释放
mediaMetadataRetriever.release();
参考:
感谢这些大大!
2、ExifInterface 获取照片拍摄参数(ISO,光圈,分辨率等)以及MediaMetadataRetriever 获取视频录制参数(时长,时间,码率,格式)