如何通过点击webview中的YouTube搜索页面链接获取视频ID

时间:2017-05-02 06:55:21

标签: android webview youtube

我有一个webview,其中我提供youtube搜索页面。 (https://www.youtube.com/results?search_query=) 我想收到用户按下的剪辑的视频ID。 到目前为止,我已经尝试了以下但没有成功:

1)使用shouldOverrideUrlLoading - 在按下搜索中的视频片段时不会被调用

public WebViewClient getWebViewClient()
{
    return new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("TAG","called overide url");
            if (url.startsWith(YOUTUBE_VIDEO_URL)) {
                setVideoIdFromLink(url);

                return true;
            }
            else
            {
                Log.d("GABI","url:"+url);
            }
            return false;
        }
        /*
        public void onLoadResource (WebView view, String url){

            if(url.startsWith(YOUTUBE_VIDEO_URL)){
                setVideoIdFromLink(url);
            }
        }*/
    };
}

2)设置onTouchListener - 方法总是返回type 0 extras null

private OnTouchListener linkClickTouch()
{
    return new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
             switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    startClickTime = Calendar.getInstance().getTimeInMillis();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                    if(clickDuration < MAX_CLICK_DURATION) {
                        v.performClick();
                    }
                }
            }
            return true;
        }
        };
    }

点击操作:

private OnClickListener linkClick()
    {

        return new OnClickListener()
        {
            public void onClick(View v) {


                final WebView webview = (WebView) v;
                final WebView.HitTestResult result = webview.getHitTestResult();
                Log.d("TAG", "result:"+result.getType()+"\nextras:"+
                result.getExtra());


                if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {

                }

                if (result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

                }

                if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) {

                }


            }
        };
    }

我知道我可以使用youtube API,但我希望在没有构建搜索活动的情况下实现这一点,只有在找到合适的搜索页面时才会显示结果。

2 个答案:

答案 0 :(得分:0)

尝试此提取Youtude Id

String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";

Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(url); //url is youtube url for which you want to extract the id.
if (matcher.find()) {
    return matcher.group();
}
webview中的

       public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("TAG","called overide url");
            if (url.startsWith(YOUTUBE_VIDEO_URL)) {
                String pattern = "(?<=watch\\v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";

                Pattern compiledPattern = Pattern.compile(pattern);
                Matcher matcher = compiledPattern.matcher(url);
                if (matcher.find()) {
                    String id = matcher.group();
                }

                return true;
            }
            else
            {
                Log.d("GABI","url:"+url);
            }
            return false;
        }

答案 1 :(得分:0)

经过多次试验和错误: 永远不会调用shouldOverrideUrlLoading或onPageStarted。

你应该使用 -

public void onLoadResource (WebView view, String url)
{
     if(url.startsWith("https://m.youtube.com/watch?"))
     {

         setVideoIdFromLink(url);
         view.stopLoading();
     }
}

public String setVideoIdFromLink(String url)
{
    //get the video id from the link
    String[] args=url.split("v=");
    String videoId=args[args.length-1];

    return videoId;
}