播放视频快进预览图完美解決方案,使用方法如下,使用FFmpeg,不影响原有播放器,
就是先截取缩略图保存下来,快进时选用图片显示,最优方案,播放性能最好,体验完美。
参考github:
https://github.com/wseemann/FFmpegMediaMetadataRetriever
下载jar
?
解压之后拷贝如下两个aar到项目libs目录下
?
使用方法:
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds
byte [] artwork = mmr.getEmbeddedPicture();
mmr.release();
代码:
package com.tianci.player.preview;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.ImageView;
import com.tianci.localmedia.util.SkyLocalContext;
import com.tianci.utils.MediaLogUtils;
import com.tianci.utils.Utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import wseemann.media.FFmpegMediaMetadataRetriever;
public class PreviewImageUtils {
private static FFmpegMediaMetadataRetriever ffmmr = null;
private static final String savePath = SkyLocalContext.getContext().getCacheDir().getPath() + "/preview/";
private String filePath = null;
private String fileName = null;
private boolean isStop = false;
private int mDuration = 0;
private String outDir = null;
private static PreviewImageUtils mInstance;
public static PreviewImageUtils getInstance()
{
if (null == mInstance)
{
synchronized (PreviewImageUtils.class)
{
if (null==mInstance)
{
mInstance = new PreviewImageUtils();
}
}
}
return mInstance;
}
public PreviewImageUtils()
{
}
private void getDuration(String url)
{
try{
if (ffmmr!=null)
{
ffmmr.release();
ffmmr = null;
}
ffmmr =new FFmpegMediaMetadataRetriever();
ffmmr.setDataSource(url);
String durationStr = ffmmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
mDuration = Integer.parseInt(durationStr)/1000;
ffmmr.release();
ffmmr = null;
}catch (Exception e)
{
e.printStackTrace();
}
}
public void startDecode(String oldUrl, String url)
{
Bitmap bitmap = null;
filePath = url;
isStop = false;
getDuration(url);
MediaLogUtils.d("startDecode-----------------url:"+url);
File file = new File(url);
if (file!=null&&file.exists())
{
fileName = file.getName();
}else {
return;
}
deleteBitmap(oldUrl);
try {
getFrameAtTime(url, mDuration, 50);
getFrameAtTime(url, mDuration, 30);
getFrameAtTime(url, mDuration, 20);
getFrameAtTime(url, mDuration, 10);
getFrameAtTime(url, mDuration, 5);
getFrameAtTime(url, mDuration, 3);
MediaLogUtils.d("startDecode------end-----------mDuration:"+mDuration);
}catch (Exception e)
{
e.printStackTrace();
ffmmr = null;
}
}
private void getFrameAtTime(String url, int duration, int step)
{
Bitmap bitmap = null;
try {
if (ffmmr!=null)
{
ffmmr.release();
ffmmr = null;
}
ffmmr =new FFmpegMediaMetadataRetriever();
ffmmr.setDataSource(url);
for (int i=0; i< duration; i+=step)
{
int pos = i*1000*1000;
if (hasFile(i, fileName))
{
continue;
}
if (ffmmr!=null)
{
bitmap = ffmmr.getScaledFrameAtTime(pos, 160, 90);
if (bitmap!=null)
saveBitmap(bitmap, i, fileName);
}
Thread.sleep(10);
if (isStop)
{
break;
}
}
ffmmr.release();
ffmmr = null;
}catch (Exception e)
{
e.printStackTrace();
}
}
private void deleteFile(String path) {
MediaLogUtils.d("deleteFile-----------------path:"+path);
File file=new File(path);
if (file!=null&&!file.exists())
{
MediaLogUtils.d("空目录");
return;
}
try
{
File[] files=file.listFiles();
if (files == null){
MediaLogUtils.d("空目录");
return;
}
for(int i =0;i<files.length;i++){
files[i].delete();
}
}catch (Exception e)
{
e.printStackTrace();
}
}
public void setStop(Boolean stop)
{
isStop = stop;
}
private void deleteBitmap(String path)
{
try {
File file = new File(path);
if (file.exists())
{
String deleteDir = savePath + file.getName();
deleteFile(deleteDir);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
private synchronized void saveBitmap(Bitmap bitmap , int position, String name) {
outDir = savePath+name;
try {
File dirFile = new File(outDir);
if (!dirFile.exists()) { //如果不存在,那就建立这个文件夹
dirFile.mkdirs();
dirFile.setWritable(true);
dirFile.setReadable(true);
Runtime.getRuntime().exec("chmod 777 -R " + dirFile.getAbsolutePath());
}
File file = new File(outDir, position+".jpg");
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
if (file.exists())
{
file.setWritable(true);
file.setReadable(true);
Runtime.getRuntime().exec("chmod 777 -R " + file.getAbsolutePath());
}
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static synchronized void setPreview(ImageView imageView, String url, int position)
{
MediaLogUtils.d("setPreview()--------------url:"+url);
MediaLogUtils.d("setPreview()--------------position:"+position);
String previewPath = null;
String name = null;
File file = new File(url);
if (file.exists())
{
name = file.getName();
}
previewPath = savePath+name+"/"+position+".jpg";
File showPicFile = new File(previewPath);
if (showPicFile!=null&&showPicFile.exists())
{
Uri uri = Uri.parse(previewPath);
imageView.setImageURI(uri);
}
}
private boolean hasFile(int position, String name)
{
try {
String filePath = savePath+name+"/"+position+".jpg";
File file = new File(filePath);
if (file!=null&&file.exists())
{
return true;
}
}catch (Exception e)
{
e.printStackTrace();
}
return false;
}
}
在播放器起播的时候使用
PreviewImageUtils.getInstance().setStop(true);
PreviewImageUtils.getInstance().startDecode(null, url);
效果图:
?
?
?
|