android获取当前视频的截图

时间:2014-02-11 09:35:33

标签: java android android-mediaplayer android-videoview

我已经看到很多关于此问题的老问题,也许现在有一些解决方案。 我想截取我的视频的当前帧的截图。 Videoview使用rtsp-stream显示实时视频。

我尝试拍摄位图,但它总是黑色

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不适用于流网址,也可能适用于视频文件。 在link上使用lib(它是MediaMetadataRetriever的包装器,可以启用 rtsp 协议输入)我可以保存一帧视频,但实际上有10秒的延迟-time videoview,因为它必须与流服务器建立新连接。

我没有测试ThumbnailUtils,但在Api中我读到输入只是文件路径

1 个答案:

答案 0 :(得分:9)

使用TextureView代替VideoView。 TextureView有getBitmap()方法。以下是TextureView作为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) {

}
}

Playing video on TextureView