纹理查看视频和位图显示 [英] Texture View video and Bitmap display

查看:319
本文介绍了纹理查看视频和位图显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示一个位图(占位符图像),并且当用户加载视频textureView应显示视频TextureView。

I have a TextureView that displays a bitmap(placeholder image) and when the user loads a video the textureView should show the video.

我设法在纹理视图视频播放和显示一个textureview一个位图,但按顺序依次进行时,我只是得到一个黑色的屏幕。

I managed to get video playback in the texture view and showing a bitmap in a textureview, but when done in sequence i just get a black screen.

在我onSurfaceTextureAvailable方法我画的后面表面上的看法位图:

In my onSurfaceTextureAvailable method i draw a bitmap on the surface view as followed:

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_add_clip);
    Canvas canvas = lockCanvas();
    canvas.drawBitmap(bm, (width/2)-(bm.getWidth()/2), (height/2)-(bm.getHeight()/2), null);
    unlockCanvasAndPost(canvas);
}

当用户从它的库我加载开放的在媒体播放器的视频和表面设置到媒体播放器。但是,位图仍显示。我试图通过清除画布去除位图,它去除位图但质地的观点是只有黑和不显示视频也是如此。

When the user selects a video from its gallery i load the Uri in the Media player and set the surface to the mediaplayer. But the bitmap is still shown. i tried to remove the bitmap by clearing the canvas, and it does remove the bitmap but then the texture view is just black and won't show the video as well.

public void setMediaPlayer(MediaPlayer mediaPlayer) {
    // Canvas canvas = lockCanvas();
    // canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    // unlockCanvasAndPost(canvas);

    Surface s = new Surface(getSurfaceTexture());
    mMediaPlayer = mediaPlayer;
    mMediaPlayer.setSurface(s);
    mMediaPlayer.setOnPreparedListener(this);
}

东西告诉我,我用来显示视频表面的看法是,我在上TextureAvailble绘制位图的后面。当我尝试清除画布我也明确表示,媒体播放器要使用的表面。任何解决方案?

Something tells me that the surface view i'm using to display the video is behind the bitmap that i draw in the on TextureAvailble. And when i try to clear the canvas i also clear the surface that the mediaplayer wants to use. Any solutions?

推荐答案

您的做法是行不通的。

表面用一个生产者 - 消费者关系缓冲区的队列。可以有一次只有一个生产者,你有两个生产者(Canvas和MediaPlayer的)。当你调用 mMediaPlayer.setSurface(),它十分重视MediaPlayer的;当你调用 lockCanvas(),它十分重视画布。需要分离的人之前可以附加其他。

The Surface is a queue of buffers with a producer-consumer relationship. There can be only one producer at a time, and you have two producers (Canvas and MediaPlayer). When you call mMediaPlayer.setSurface() it attaches MediaPlayer; when you call lockCanvas() it attaches Canvas. You need to detach one before you can attach the other.

(检查logcat的输出 - 有可能是有某种已连接的投诉)

(Check your logcat output -- there's probably some "already connected" complaints in there.)

麻烦的是,当你应该能够分离的MediaPlayer,你不能脱离画布。有根本没有调用做的。这是API在一个不幸的神器。 (确认了通过4.4奇巧,我怀疑,因为它已经改变了。)

The trouble is, while you should be able to detach MediaPlayer, you can't detach Canvas. There's simply no call that does it. This is an unfortunate artifact in the API. (Confirmed up through 4.4 "KitKat", and I doubt it has changed since.)

您有几种选择:


  1. 绘制与GLES位图来代替。你可以看到在Grafika的一个例子<一个href=\"https://github.com/google/grafika/blob/master/src/com/android/grafika/PlayMovieSurfaceActivity.java\"相对=nofollow> PlayMovieSurface活动 - 请参阅 clearSurface()方法,刚刚清除屏幕。随着GLES,可以附加从表面分离。

  2. 转换位图到一个单一帧视频,并通过的MediaPlayer播放。

  3. 把一个ImageView的上用的FrameLayout视频窗口的上方。隐藏的ImageView,而视频播放。

  1. Draw the bitmap with GLES instead. You can see an example in Grafika's PlayMovieSurface activity -- see the clearSurface() method, which just clears the screen. With GLES, you can attach and detach from the surface.
  2. Convert your bitmap into a single-frame video and play it through MediaPlayer.
  3. Put an ImageView on top of the video window with a FrameLayout. Hide the ImageView while the video is playing.

我要补充一点,我还没有真正尝试从表面分离一MediaPlayer的。较低级别的媒体codeC以这种方式工作,如由Grafika的球员,所以我假设的MediaPlayer也将分离干净。为您的使用情况下,虽然,#3可能是最简单的解决方案,无需安装/拆卸体操。

I should add that I haven't actually tried detaching a MediaPlayer from a Surface. The lower-level MediaCodec works this way, as shown by the Grafika player, so I'm assuming MediaPlayer will also detach cleanly. For your use case, though, #3 is probably the easiest solution, and requires no attach/detach gymnastics.

这篇关于纹理查看视频和位图显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆