目前videoview的Andr​​oid开截图 [英] android get screenshot of current videoview

查看:748
本文介绍了目前videoview的Andr​​oid开截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多关于这个老问题,也许现在也有一些解决方案。 我想利用我的videoview当前帧的屏幕截图。 Videoview示出了使用RTSP流一个实时视频

I've seen a lot of old questions about this, maybe now there are some solutions. I want to take a screenshot of the current frame of my videoview. Videoview shows a real-time video using a rtsp-stream.

我试图把位图,但它总是黑

I try to take bitmap, but it's always black

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width , v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}

编辑: 类MediaMetadataRetriever 不与流URL的工作,也许可以与视频文件。 使用LIB在此链接(这是类MediaMetadataRetriever ,使 RTSP 协议输入),我可以节省视频的帧,但有10秒的延迟尊重实时videoview,因为它必须建立与流媒体服务器的新连接。

EDIT : MediaMetadataRetriever does not work with stream url, maybe works with video-file. Using lib at this link (it's a wrapper of MediaMetadataRetriever that enable rtsp protocol input) I can save a frame of video, but there is a delay of 10 secs respect real-time videoview because it must create a new connection with streaming server.

我没有测试 ThumbnailUtils ,但在阿比我读到输入唯一的文件路径

I not test ThumbnailUtils, but in Api I read that input is only file-path

推荐答案

使用TextureView而不是VideoView。 TextureView具有getBitmap()方法。这里是TextureView的用法videoView

Use TextureView instead VideoView. TextureView has getBitmap() method. Here is the usage of TextureView as videoView

public class TextureVideoActivity extends Activity implements TextureView.SurfaceTextureListener {

private static final String FILE_NAME = "myVideo.mp4";
private static final String TAG = TextureVideoActivity.class.getName();

private MediaPlayer mMediaPlayer;
private TextureView mPreview;

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.activity_texture_video);

    mPreview = (TextureView) findViewById(R.id.textureView);
    mPreview.setSurfaceTextureListener(this);

}

public Bitmap getBitmap(){
   return  mPreview.getBitmap();
}

public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    Surface surface = new Surface(surfaceTexture);

    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer
                .setDataSource(this, Uri.parse(FILE_NAME));
        mMediaPlayer.setSurface(surface);
        mMediaPlayer.setLooping(true);

        // don't forget to call MediaPlayer.prepareAsync() method when you use constructor for
        // creating MediaPlayer
        mMediaPlayer.prepareAsync();
        // Play video when the media source is ready for playback.
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });

    } catch (IllegalArgumentException e) {
        Log.d(TAG, e.getMessage());
    } catch (SecurityException e) {
        Log.d(TAG, e.getMessage());
    } catch (IllegalStateException e) {
        Log.d(TAG, e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mMediaPlayer != null) {
        // Make sure we stop video and release resources when activity is destroyed.
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {

}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
    return false;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

}
}

上TextureView 播放视频

这篇关于目前videoview的Andr​​oid开截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆