小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直播流,具有实时性)
本文来自HowNoon分享,HowNoon的blog地址为:http://blog.csdn.net/yuehenhn。
videoview视频播放 http://blog.csdn.net/shenxiaolei507/article/details/41046345
MediaPlayer+SurfaceView视频播放 http://blog.csdn.net/shenxiaolei507/article/details/41349295
1
videoView.getDrawingCache();获取截图
2
videoView.setDrawingCacheEnabled(true);
bitmap = videoView.getDrawingCache();获取截图
http://bbs.csdn.net/topics/390185458
http://bbs.csdn.net/topics/390120655
3
MediaMetaDataRetreive获取截图
public static Bitmap getCurrentVideoBitmap(String url,VideoView videoView){
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
try {
mediaMetadataRetriever.setDataSource(url,new HashMap<String, String>());
Log.e(LOG_STRING,"截图的时间为"+videoView.getCurrentPosition());
//取得指定时间的Bitmap,即可以实现抓图(缩略图)功能
bitmap = mediaMetadataRetriever.getFrameAtTime(videoView.getCurrentPosition()*1000,MediaMetadataRetriever.OPTION_CLOSEST);
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
mediaMetadataRetriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
if (bitmap == null) {
return null;
}
//bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
bitmap = Bitmap.createBitmap(bitmap);
return bitmap;
}
4
OPTION_CLOSEST_SYNC参数换为OPTION_CLOSEST
例如: http://blog.sina.com.cn/s/blog_7bac47070101bys8.html
http://yashirocc.blog.sohu.com/175636801.html
以下为各参数详解
参看资料:
5
寻找第三方框架截图
Vitamio https://www.vitamio.org/ 国人开发的框架
导入代码测试,这个框架好像将videoview封装了一层,叫mVideoview,据官网介绍,截图方法为getCurrentFrame();不过据博主测试,返回值为空。
vlc for android http://blog.csdn.net/banketree/article/details/39575973
导入代码进行测试,发现该框架代码里默认为rtps协议视频,并且封装了C++的so文件,使用到了JNI技术,截图什么的还要自己打补丁,看了看各位大神的文章介绍,看起来好复杂,本人水平有限,就默默放弃了。
6
TextureView播放视频并截图
附上链接: http://stackoverflow.com/questions/27435985/how-to-capture-screenshot-of-videoview-when-streaming-video-in-android How to capture screenshot of VideoView when streaming video in Android [duplicate]
附上回答:
附上翻译:
**我已经找到了解决这个问题的办法。看来VideoView不允许因为底层硬件GPU的原因而使用SurfaceView。
该解决方案是使用一个textureview使用MediaPlayer播放视频里面。该活动将需要实现textureview.surfacetexturelistener。当用这个解决方案的截图,视频冻结了一段时间。同时,该textureview不显示播放进度条默认UI(播放,暂停,快进/ RW,玩的时间,等)。这是一个缺点。如果你有另一个解决方案,请让我知道:)**
public class TextureViewActivity extends Activity
implements TextureView.SurfaceTextureListener,
OnBufferingUpdateListener,
OnCompletionListener,
OnPreparedListener,
OnVideoSizeChangedListener {
private MediaPlayer mp; private TextureView tv; public static String MY_VIDEO = "https://www.blahblahblah.com/myVideo.mp4"; public static String TAG = "TextureViewActivity"; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_texture_view);
tv = (TextureView) findViewById(R.id.textureView1);
tv.setSurfaceTextureListener(this);
} public void getBitmap(TextureView vv)
{
String mPath = Environment.getExternalStorageDirectory().toString()
+ "/Pictures/" + Utilities.getDayTimeString() + ".png";
Toast.makeText(getApplicationContext(), "Capturing Screenshot: " + mPath, Toast.LENGTH_SHORT).show();
Bitmap bm = vv.getBitmap(); if(bm == null)
Log.e(TAG,"bitmap is null");
OutputStream fout = null;
File imageFile = new File(mPath); try {
fout = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.PNG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "IOException");
e.printStackTrace();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.media_player_video, menu); return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); if (id == R.id.action_settings) { return true;
} return super.onOptionsItemSelected(item);
} @Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
Surface s = new Surface(surface); try
{
mp = new MediaPlayer();
mp.setDataSource(MY_VIDEO);
mp.setSurface(s);
mp.prepare();
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setOnVideoSizeChangedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
Button b = (Button) findViewById(R.id.textureViewButton);
b.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v)
{
TextureViewActivity.this.getBitmap(tv);
}
});
} catch (IllegalArgumentException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
第一时间获得博客更新提醒,以及更多 android,源码分析,最新开源项目推荐,更多有价值的思考 ,欢迎关注我的微信公众号,扫一扫下方二维码或者长按识别二维码,即可关注。