Youtube内部的webview在android应用程序

Youtube inside webview in android application

本文关键字:android 应用程序 webview 内部 Youtube      更新时间:2023-09-26

在阅读了不同的例子后,我几乎达到了想要的结果

代码:

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)
import android.app.Instrumentation;
...
private Instrumentation mInstrumentation;
....
public void onCreate(...) {
    ...
    mInstrumentation = new Instrumentation();
    ....
}
// When the page is ready.
final int cursorXPos = CENTER_OF_SCREEN_X_POS;
final int cursorYPos = CENTER_OF_SCREEN_Y_POS;
new Thread() {
    public void run() {
        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));
}
        }.start();