Android如何在webview上播放和浏览youtube视频

时间:2012-12-08 07:15:18

标签: android youtube android-webview

我正在Android中开发一个应用程序,我应该可以在webview中传递一个youtube网址并进行游戏和浏览。到目前为止的进展是我可以看到初始屏幕,但播放按钮不播放视频。  在模拟器中它仍然播放声音,但在设备中声音也没有播放,虽然我可以浏览youtube视频并执行所有其他功能。我发布下面的代码:

    public class YoutubePlayActivity extends Activity {
    String myUrl = "http://www.youtube.com/watch?v=wvwnjWcG250&feature=related";
     wv1 = (WebView) findViewById(R.id.web);
    wv1.setWebViewClient(new myClient());
    wv1.loadUrl("http://www.youtube.com/watch?v=wvwnjWcG250&feature=related");
    wv1.getSettings().setJavaScriptEnabled(true);
    wv1.getSettings().setSupportZoom(true); 

    wv1.getSettings().setBuiltInZoomControls(true);

    wv1.getSettings().setPluginState(WebSettings.PluginState.ON);
    wv1.setWebChromeClient(new MyWebChromeClient());
    // TODO Auto-generated method stub
}

class myClient extends WebViewClient {
    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        // TODO Auto-generated method stub
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

}
@Override
public void onPause() {
    super.onPause();

    try {
        Class.forName("android.webkit.WebView")
                .getMethod("onPause", (Class[]) null)
                            .invoke(wv1, (Object[]) null);

    } catch(ClassNotFoundException cnfe) {

    } catch(NoSuchMethodException nsme) {

    } catch(InvocationTargetException ite) {

    } catch (IllegalAccessException iae) {

    }
}
public class MyWebChromeClient extends WebChromeClient {

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        // TODO Auto-generated method stub
        super.onShowCustomView(view, callback);
        if (view instanceof FrameLayout) {
            FrameLayout frame = (FrameLayout) view;
            if (frame.getFocusedChild() instanceof VideoView) {
                VideoView video = (VideoView) frame.getFocusedChild();
                frame.removeView(video);
                video.start();
            }
        }

    }
}

}

0 个答案:

没有答案