How do I draw Bitmap on TextureView

10k views Asked by At

I'm trying to play video on android with TextureView. Regarding video everithing works well. But MediaPlayer.prepare(); takes some time. So I would like to show some placeholder for that time. I'm trying to do it with Glide like this:

public static final Rect rc = new Rect();
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int w, int h) {
    Surface s = new Surface(surfaceTexture);
    Glide.with(mTextureView.getContext())
            .load(thumbUrl)
            .asBitmap()
            .dontAnimate()
            .centerCrop()
            .into(new SimpleTarget<Bitmap>(w, h) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    rc.set(0,0,w,h);
                    Canvas c = mTextureView.lockCanvas(rc);
                    if (c == null)
                        return;
                    c.drawBitmap(resource, 0, 0, null);
                    mTextureView.unlockCanvasAndPost(c);
                }
            });

    try {
        mMediaPlayer.setDataSource(url);
        mMediaPlayer.setSurface(s);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
                mMediaPlayer.setVolume(0,0);
                mMediaPlayer.start();
            }
        });
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

But it does not show any picture. I case of SurfaceView we need to call onDraw manualy. And SurfaceHolder has been used for this. But TextureView does not have getSurfaceHolder() method.

What is the right way of doing this ?

UPD: What I figured out is that picture is displayed if I do not set up MediaPlayer it means that MediaPlayer keeps surface black while it is buffering video. Is it possible to override this behavior somehow ?

0

There are 0 answers