我将 TextureView 设置为 MetoaPlayer 来播放视频:
TextureView textureView = new TextureView(context);
addView(textureView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
textureView.setSurfaceTextureListener(this);
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
{
return false;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
this.surface = new Surface(surface);
mediaPlayer.setSurface(this.surface);
prepareAndPlay();
}
视频播放正常。
但是当我尝试在表面上绘制很多时,视频播放没有开始!
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
this.surface = new Surface(surface);
mediaPlayer.setSurface(this.surface);
// DRAW FIRST FRAME ON SURFACE
Bitmap bitmap = BitmapFactory.decodeFile(firstFramePath);
Canvas canvas = surface.lockCanvas(null);
canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), null);
bitmap.recycle();
surface.unlockCanvasAndPost(canvas);
prepareAndPlay();
}
当我在媒体播放器上调用 play() 时,播放没有开始。
我想,Surface 移动到无效状态并且 MediaPlayer 无法播放视频。但是 logcat 是空的。有任何方法可以使用媒体播放器在同一表面上绘图。