MediaUtils.java
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.chudiangameplay.android.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
/**
* Created by Administrator on 2019/2/20.
*/
public class MediaUtils {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static File file;
/**
* 获取视频的第一帧图片
*/
public static void getImageForVideo(String videoPath, OnLoadVideoImageListener listener) {
LoadVideoImageTask task = new LoadVideoImageTask(listener);
task.execute(videoPath);
}
public static class LoadVideoImageTask extends AsyncTask<String, Integer, Bitmap> {
private OnLoadVideoImageListener listener;
public LoadVideoImageTask(OnLoadVideoImageListener listener) {
this.listener = listener;
}
@Override
protected Bitmap doInBackground(String... params) {
try {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
String path = params[0];
if (path.startsWith("http"))
//获取网络视频第一帧图片
mmr.setDataSource(path, new HashMap());
else
//本地视频
mmr.setDataSource(path);
Bitmap bitmap = mmr.getFrameAtTime();
mmr.release();
return bitmap;
}
catch (Exception e)
{
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (listener != null) {
listener.onLoadImage(bitmap);
}
}
}
public interface OnLoadVideoImageListener {
void onLoadImage(Bitmap bitmap);
}
}