您的位置:首页 > 移动开发 > Android开发

android4.2 图库修剪视频后,详细信息中不显示时长

2013-08-15 10:22 183 查看
原因:

视频修改完成后没有获取剪辑视频时间,并插入数据库

TrimVideo.java

public void run() {

try {

double trimVideoDuration =
TrimVideoUtils.startTrim(mSrcFile, mDstFile, mTrimStartTime, mTrimEndTime);

// Update the database for adding a new video file.

insertContent(mDstFile, (int)(trimVideoDuration * 1000));

} catch (IOException e) {

.................

private Uri insertContent(File file,
int duration) {

long nowInMs = System.currentTimeMillis();

long nowInSec = nowInMs / 1000;

final ContentValues values = new ContentValues(13);

values.put(Video.Media.TITLE, mSaveFileName);

values.put(Video.Media.DISPLAY_NAME, file.getName());

values.put(Video.Media.MIME_TYPE, "video/mp4");

values.put(Video.Media.DATE_TAKEN, nowInMs);

values.put(Video.Media.DATE_MODIFIED, nowInSec);

values.put(Video.Media.DATE_ADDED, nowInSec);

values.put(Video.Media.DATA, file.getAbsolutePath());

values.put(Video.Media.SIZE, file.length());

values.put(Video.Media.DURATION, duration);

// Copy the data taken and location info from src.

TrimVideoUtils.java

public static double startTrim(File src, File dst, int startMs, int endMs) throws IOException

....

double TrimVideoStartTime = Double.MAX_VALUE;

double TrimVideoEndTime = 0;

for (Track track : tracks) {

long currentSample = 0;

double currentTime = 0;

long startSample = -1;

long endSample = -1;

double trackStartTime = 0;

double trackEndTime = 0;

.......

if (currentTime <= startTime) {

// current sample is still before the new starttime

startSample = currentSample;

trackStartTime = currentTime;

}

if (currentTime <= endTime) {

// current sample is after the new start time and still before the new endtime

endSample = currentSample;

trackEndTime = currentTime;

} else {

// current sample is after the end of the cropped video

break;

}

currentTime += (double) entry.getDelta() / (double) track.getTrackMetaData().getTimescale();

currentSample++;

}

}

TrimVideoStartTime = Math.min(TrimVideoStartTime, trackStartTime);

TrimVideoEndTime = Math.max(TrimVideoEndTime,trackEndTime);

movie.addTrack(new CroppedTrack(track, startSample, endSample));

}

IsoFile out = new DefaultMp4Builder().build(movie);

if (!dst.exists()) {

dst.createNewFile();

}

FileOutputStream fos = new FileOutputStream(dst);

FileChannel fc = fos.getChannel();

out.getBox(fc); // This one build up the memory.

fc.close();

fos.close();

randomAccessFile.close();

return TrimVideoEndTime - TrimVideoStartTime;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: