Skip to content

Instantly share code, notes, and snippets.

@WanghongLin
Created December 26, 2014 02:13
Show Gist options
  • Save WanghongLin/bbe293c2f5794721c2b1 to your computer and use it in GitHub Desktop.
Save WanghongLin/bbe293c2f5794721c2b1 to your computer and use it in GitHub Desktop.
Get webview html5 video data source from Java reflections
import android.net.Uri;
import android.util.Log;
import android.webkit.WebView;
/**
* using java reflect method to get html5 video data source
*/
public class WebViewDataSourceReflect {
private static final String TAG = WebViewDataSourceReflect.class.getSimpleName();
private WebViewDataSourceReflect() {
// TODO Auto-generated constructor stub
throw new AssertionError();
}
public static String getVideoDataSource(WebView webView) {
if (webView == null) {
return null;
}
try {
Field providerField = WebView.class.getDeclaredField("mProvider");
providerField.setAccessible(true);
Object providerObject = providerField.get(webView);
if (providerObject != null) {
Log.d(TAG, "get provider successfully " + providerObject.getClass().getName());
}
Class<?> webWebClassicClass = Class.forName("android.webkit.WebViewClassic");
Field html5VideoProxyField = webWebClassicClass.getDeclaredField("mHTML5VideoViewProxy");
html5VideoProxyField.setAccessible(true);
Object html5VideoProxyObject = html5VideoProxyField.get(providerObject);
if (html5VideoProxyObject != null) {
Log.d(TAG, "get video proxy successfully " + html5VideoProxyObject.getClass().getName());
}
Class<?> html5VideoProxyClass = Class.forName("android.webkit.HTML5VideoViewProxy");
for (Class<?> declaredClass: html5VideoProxyClass.getDeclaredClasses()) {
if (declaredClass.getName().equals("android.webkit.HTML5VideoViewProxy$VideoPlayer")) {
Field html5VideoViewField = declaredClass.getDeclaredField("mHTML5VideoView");
html5VideoViewField.setAccessible(true);
Object html5VideoViewObject = html5VideoViewField.get(new Object());
if (html5VideoViewObject != null) {
Log.d(TAG, "get video view successfully " + html5VideoViewObject.getClass().getName());
Class<?> html5VideoViewClass = Class.forName("android.webkit.HTML5VideoView");
Field mediaPlayerField = html5VideoViewClass.getDeclaredField("mUri");
mediaPlayerField.setAccessible(true);
Uri uri = (Uri) mediaPlayerField.get(html5VideoViewObject);
return uri.toString();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
@WanghongLin
Copy link
Author

This code snippet will return video's url after playback begin in Android webview

@saqada
Copy link

saqada commented Jan 25, 2016

WanghongLin, thank you very much. But what to do for devices which have higher API (API 17 +) ? These devices use com.android.webview.chromium, and the reflection returns ClassNotFound exception for "android.webkit.WebViewClassic" and "android.webkit.HTML5VideoView". . I'm looking forward for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment