我试图用TextureView
在Android上播放视频。关于视频任务运作良好。但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
在缓冲视频时保持表面黑色。是否有可能以某种方式覆盖此行为?