在阅读了不同的例子之后,我几乎达到了想要的结果
编码 :
WebView myWebView = (WebView)findViewById(R.id.webview);
String strHTMLTags = "<iframe width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/MwdUyxyRwc8?autoplay=1\" frameborder=\"0\" allowfullscreen=\"1\" mozallowfullscreen=\"1\" webkitallowfullscreen=\"1\"></iframe>";
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient() {});
myWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadData(strHTMLTags, "text/html", "utf-8");
这将使代码在自动启动旁边工作得很好。
如果我在“loadData”之前添加此代码:
webSettings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setAllowFileAccess(true);
此代码适用于自动启动,但分辨率不正确,视频看起来不像他应该的那样。
setUserAgentString 究竟做了什么,我怎样才能让他使用 autostart 。
我尝试另一种方式:
public void onPageFinished(WebView view, String url) {
int cursorXPos = (int) (view.getX() + view.getWidth()/2);
int cursorYPos = (int) (view.getX() + view.getHeight()/2);
int cursorXPos = CENTER_OF_SCREEN_X_POS;
int cursorYPos = CENTER_OF_SCREEN_Y_POS;
mInstrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, cursorXPos,
cursorYPos, 0));
mInstrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_UP, cursorXPos,
cursorYPos, 0));
}
});
并得到错误:
05-05 15:33:52.135: E/AndroidRuntime(1334): FATAL EXCEPTION: main
05-05 15:33:52.135: E/AndroidRuntime(1334): java.lang.RuntimeException: This method can not be called from the main application thread
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1555)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.app.Instrumentation.sendPointerSync(Instrumentation.java:904)
05-05 15:33:52.135: E/AndroidRuntime(1334): at com.example.embeded.MainActivity$3.onPageFinished(MainActivity.java:69)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:317)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.os.Looper.loop(Looper.java:137)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-05 15:33:52.135: E/AndroidRuntime(1334): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 15:33:52.135: E/AndroidRuntime(1334): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 15:33:52.135: E/AndroidRuntime(1334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-05 15:33:52.135: E/AndroidRuntime(1334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-05 15:33:52.135: E/AndroidRuntime(1334): at dalvik.system.NativeStart.main(Native Method)