【问题标题】:How to search Youtube videos in WebView?如何在 WebView 中搜索 Youtube 视频?
【发布时间】:2015-01-10 15:32:37
【问题描述】:

我有一个带有两个活动的 Android 应用程序:一个是 Youtube Webview,另一个是从键盘获取字符串的 mainactivity。

有没有什么方法可以在 Youtube 中显示我启动 Webview 活动时给出的字符串的搜索?

谢谢

【问题讨论】:

    标签: android webview youtube


    【解决方案1】:

    是的,您可以在 webview 中搜索和播放视频

    public class UniversalWebViewFragment extends Fragment {
    
        private static final String GOOGLE_SERACH_URL = "https://www.google.com/search?q=";
    
        private static final String YOUTUBE_SERACH_URL = "https://www.youtube.com/results?search_query=android";
    
        private WebView webView;
        private FrameLayout customViewContainer;
        private WebChromeClient.CustomViewCallback customViewCallback;
        private View mCustomView;
        private myWebChromeClient mWebChromeClient;
        private myWebViewClient mWebViewClient;
    
        public static UniversalWebViewFragment newInstance() {
    
            return new UniversalWebViewFragment();
        }
    
        public void searchOnGoogle(String key) {
            webView.loadUrl(GOOGLE_SERACH_URL + key);
        }
    
        public void searchOnYoutube(String key) {
            webView.loadUrl(YOUTUBE_SERACH_URL + key);
        }
    
        /**
         * Called when the activity is first created.
         */
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.universal_web_view,
                    container, false);
    
            customViewContainer = (FrameLayout) rootView
                    .findViewById(R.id.customViewContainer);
            webView = (WebView) rootView.findViewById(R.id.webView);
    
            mWebViewClient = new myWebViewClient();
            webView.setWebViewClient(mWebViewClient);
    
            mWebChromeClient = new myWebChromeClient();
            webView.setWebChromeClient(mWebChromeClient);
            webView.getSettings().setJavaScriptEnabled(true);
    
            // Important for PayUMoney
            webView.getSettings().setDomStorageEnabled(true);
    
            webView.getSettings().setAppCacheEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSaveFormData(true);
            webView.loadUrl(GOOGLE_SERACH_URL);
            // webView.requestFocus();
    
            // Handle Back keyPress
            rootView.setFocusableInTouchMode(true);
            rootView.requestFocus();
            rootView.setOnKeyListener(new OnKeyListener() {
    
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
    
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
    
                        return true;
    
                    }
                    // return super.onKeyDown(keyCode, event);
                    return true;
                }
            });
    
            return rootView;
        }
    
        public boolean inCustomView() {
            return (mCustomView != null);
        }
    
        public void hideCustomView() {
            mWebChromeClient.onHideCustomView();
        }
    
        @Override
        public void onPause() {
            super.onPause(); // To change body of overridden methods use File |
                                // Settings | File Templates.
            webView.onPause();
        }
    
        @Override
        public void onResume() {
            super.onResume(); // To change body of overridden methods use File |
                                // Settings | File Templates.
            webView.onResume();
        }
    
        @Override
        public void onStop() {
            super.onStop(); // To change body of overridden methods use File |
                            // Settings | File Templates.
            if (inCustomView()) {
                hideCustomView();
            }
        }
    
        class myWebChromeClient extends WebChromeClient {
            private View mVideoProgressView;
    
            @Override
            public void onShowCustomView(View view, int requestedOrientation,
                    CustomViewCallback callback) {
                onShowCustomView(view, callback); // To change body of overridden
                                                    // methods use File | Settings |
                                                    // File Templates.
            }
    
            @Override
            public void onShowCustomView(View view, CustomViewCallback callback) {
    
                // if a view already exists then immediately terminate the new one
                if (mCustomView != null) {
                    callback.onCustomViewHidden();
                    return;
                }
                mCustomView = view;
                webView.setVisibility(View.GONE);
                customViewContainer.setVisibility(View.VISIBLE);
                customViewContainer.addView(view);
                customViewCallback = callback;
            }
    
            @Override
            public View getVideoLoadingProgressView() {
    
                if (mVideoProgressView == null) {
                    LayoutInflater inflater = LayoutInflater.from(getActivity());
                    mVideoProgressView = inflater.inflate(R.layout.video_progress,
                            null);
                }
                return mVideoProgressView;
            }
    
            @Override
            public void onHideCustomView() {
                super.onHideCustomView(); // To change body of overridden methods
                                            // use File | Settings | File Templates.
                if (mCustomView == null)
                    return;
    
                webView.setVisibility(View.VISIBLE);
                customViewContainer.setVisibility(View.GONE);
    
                // Hide the custom view.
                mCustomView.setVisibility(View.GONE);
    
                // Remove the custom view from its container.
                customViewContainer.removeView(mCustomView);
                customViewCallback.onCustomViewHidden();
    
                mCustomView = null;
            }
        }
    
        class myWebViewClient extends WebViewClient {
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
    
            private int webViewPreviousState;
    
            private final int PAGE_STARTED = 0x1;
    
            private final int PAGE_REDIRECTED = 0x2;
    
            Dialog dialog = new Dialog(getActivity());
    
            /*
             * (non-Javadoc)
             * 
             * /* (non-Javadoc)
             * 
             * @see
             * android.webkit.WebViewClient#onPageStarted(android.webkit.WebView,
             * java.lang.String, android.graphics.Bitmap)
             */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                webViewPreviousState = PAGE_STARTED;
    
                if (dialog == null || !dialog.isShowing())
                    dialog = ProgressDialog.show(getActivity(), "",
                            "Loading Please Wait", true, true,
                            new OnCancelListener() {
    
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    // do something
                                }
                            });
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
    
                if (webViewPreviousState == PAGE_STARTED) {
                    if (null != dialog)
                        dialog.dismiss();
                    dialog = null;
                }
    
            }
        }
    
    }
    

    这是 youtube 的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <WebView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/webView"
                android:layout_gravity="center"
                />
        <FrameLayout
                android:id="@+id/customViewContainer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:visibility="gone"
                />
    </LinearLayout>
    

    youtube 进度布局

      <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/progress_indicator"
                      android:orientation="vertical"
                      android:layout_centerInParent="true"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent">
    
            <ProgressBar android:id="@android:id/progress"
                         style="?android:attr/progressBarStyleLarge"
                         android:layout_gravity="center"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"/>
    
            <TextView android:paddingTop="5dip"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:text="loading"
                      android:textSize="14sp"
                      android:textColor="?android:attr/textColorPrimary"/>
        </LinearLayout>
    

    这样使用

    universalWebViewFragment = UniversalWebViewFragment.newInstance();
    getFragmentManager().beginTransaction()
            .add(R.id.frame_root, universalWebViewFragment).commit();
    

    搜索这样的视频

                        universalWebViewFragment.searchOnYoutube(searchView
                                .getText().toString());
    

    别忘了添加权限

     <uses-permission android:name="android.permission.INTERNET" />
    

    在这里找到完整的项目https://github.com/hiteshsahu/UniversalWebPageLoader

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 2015-07-12
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 2013-07-20
      相关资源
      最近更新 更多