0

I have TextureView that I set to MetoaPlayer to play video:

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();
}

Video plays normally.

But when I try to draw manyally on surface, video playing not started!

@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();
}

When I call play() on mediaplayer playing not started.

I suppose, that Surface move to invalid state and MediaPlayer cannot play video. But logcat is empty. There is any way to draw on same surface both with media player.

Nik
  • 7,114
  • 8
  • 51
  • 75

1 Answers1

3

You can't do this.

A "surface" is the producer side of a producer-consumer pair. You can't have two producers. (If you want the gory details, see this doc.)

With a TextureView you can change the underlying SurfaceTexture using setSurfaceTexture(), which should allow you to switch from one to the other. This feature is used (for different reasons) in Grafika's "double decode" activity.

fadden
  • 51,356
  • 5
  • 116
  • 166