10

我正在尝试在 android 上播放视频TextureView。关于视频,一切都很好。但MediaPlayer.prepare();需要一些时间。所以我想展示一些当时的占位符。我正在尝试像这样使用 Glide 来做到这一点:

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

但它没有显示任何图片。我的情况是SurfaceView我们需要手动调用onDraw。SurfaceHolder 已用于此目的。但TextureView没有getSurfaceHolder()办法。

这样做的正确方法是什么?

UPD:我发现如果我不设置图片就会显示,MediaPlayer这意味着MediaPlayer在缓冲视频时保持表面黑色。是否有可能以某种方式覆盖这种行为?

4

0 回答 0