Android-在SurfaceTexture上绘制YouTube视频 [英] Android - draw YouTube video on SurfaceTexture

查看:306
本文介绍了Android-在SurfaceTexture上绘制YouTube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SurfaceTexture上绘制WebView,以便可以在OpenGL中对其进行渲染.

I'm trying to draw a WebView on SurfaceTexture so I can render it in OpenGL.

到目前为止,我已经成功以标准方式在WebView中播放了youtube视频:

So far, I successfully played youtube video in WebView the standard way:

  ...
  webView.getSettings().setJavaScriptEnabled(true);

  if (Build.VERSION.SDK_INT < 8) 
  {
     webView.getSettings().setPluginsEnabled(true);
  } 
  else 
  {
     webView.getSettings().setPluginState(WebSettings.PluginState.ON);
  }
  webView.setWebChromeClient(new WebChromeClient() { });
  webView.setWebViewClient(new WebViewClient());
  ...

还打开了硬件加速:

 <application
        android:hardwareAccelerated="true"
 ...

并且我还根据此教程在OpenGL中成功渲染了WebView(播放视频除外):

And I also successfully rendered the WebView in OpenGL (except playing the video) based on this tutorial: http://anuraagsridhar.wordpress.com/2013/03/13/rendering-an-android-webview-or-for-that-matter-any-android-view-directly-to-opengl/

WebView的其他初始化(因此始终为纵向"并适合纹理):

Additional initialization of the WebView (so it is always 'portrait' and fits into the texture):

 Point p = new Point();
 activity.getWindow().getWindowManager().getDefaultDisplay().getSize(p);
 webView.setLayoutParams(new ViewGroup.LayoutParams(p.x, p.y));
 surfaceTexture.setDefaultBufferSize(p.x, p.y);

覆盖WebView的onDraw方法:

Overriden onDraw method of the WebView:

@Override
public void onDraw(Canvas c)
{
   try
   {
      Point p = new Point();
      activity.getWindow().getWindowManager().getDefaultDisplay().getSize(p);
      Canvas canvas = surfaceTexture.lockCanvas(new Rect(0,0,p.x,p.y));
      canvas.save();
      canvas.translate(-scrollL, -scrollT);
      super.onDraw(canvas);
      canvas.restore();
      surfaceTexture.unlockCanvasAndPost(canvas);
   }
   catch (Surface.OutOfResourcesException e)
   {
      throw new RuntimeException(e);
   }
}

当尝试使用上述技巧在Webview上播放YouTube视频时,视频矩形为黑色(但是可以听到声音). 我怀疑硬件加速不适用于表面纹理.

When trying to play YouTube video on webview rendered using the above hack, the video rectangle is black (however the sound can be heard). I suspect that the hardware acceleration does not work on surface texture.

所以问题是: 有什么方法可以在OpenGL纹理上播放youtube视频吗?

So the question is: Is there any way to play a youtube video on OpenGL texture?

推荐答案

问题是这样的:视频正被绘制到视图层次结构窗口后面的单独窗口中,并且很难在.使问题复杂化的是,WebView的位置和大小已确定了视频窗口的大小,而不考虑将其自身绘制到屏幕上将显示的Canvas之外的其他情况.此外,视频是用本机代码呈现的,因此您无法以足够快的速度绘制到Canvas来跟上(这就是为视频提供单独窗口的全部思想).

The problem is this: the video is being drawn to a separate window that is behind the view hierarchy's window and is not readily accessible to be drawn on your SurfaceTexture. To complicate the matter, the WebView will have positioned and sized the video window without regard for itself being drawn to a Canvas other than the one that would be displayed on the screen. Furthermore, the video is being rendered in native code, and you wouldn't be able to draw to a Canvas fast enough to keep up anyway (that's the whole idea behind giving the video a separate window).

任何令人满意的解决方案几乎都必须使用私有API才能以本机代码访问视频窗口.根据您计划使用SurfaceTexture的方式,您可能会想出一种解决方法,包括拦截WebViewClient中的URL,在渲染前去除所有视频标签,以及将视频放入SurfaceViewTextureView在您的控制之下.然后,您可以将其放置在与绘制SurfaceTexture的位置相关的位置,但这将SurfaceTexture的使用限制为布告栏.不过,这种骇客很快就会变得非常丑陋.

Any satisfactory solution would almost necessarily require use of the private APIs to get access to the video window in native code. Depending on how you plan to use your SurfaceTexture, you might come up with a workaround that involves intercepting URLs in a WebViewClient, stripping out any video tags before rendering, and placing the video in a SurfaceView or TextureView under your control. You can then put it where it should be in relation to where you've drawn the SurfaceTexture, but that limits your usage of the SurfaceTexture to billboarding. That sort of hack would get very ugly very quickly, though.

简单地说,我看不出这样做的干净方法.这是一个好主意,但不适用于公共API.

Put simply, I don't see a clean way to do this. It's a nice idea, but it's just not workable with the public API.

这篇关于Android-在SurfaceTexture上绘制YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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