Android - draw YouTube video on SurfaceTexture

admin

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

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

Code:
  ...
  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());
  ...

also the hardware acceleration is turned on:

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

And I also successfully rendered the WebView in OpenGL (except playing the video) based on this tutorial: <a href="http://anuraagsridhar.wordpress.com...t-matter-any-android-view-directly-to-opengl/" rel="noreferrer">http://anuraagsridhar.wordpress.com...t-matter-any-android-view-directly-to-opengl/</a>

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

Code:
 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);

Overriden onDraw method of the WebView:

Code:
@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);
   }
}

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.

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