how to get video id from clicking youtube search page link in a webview

Gabriel H

I have a webview in which i present youtube search page. (https://www.youtube.com/results?search_query=) i want to receive the video id of the clip the user pressed. so far i have tried the following without success:

1)use of shouldOverrideUrlLoading - isnt being called when pressing on the video clip in search

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)setting an onTouchListener - method always return 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;
        }
        };
    }

the click action:

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) {

                }


            }
        };
    }

i know i can probably use the youtube API but i want to accomplish that without building a search activity wich will display results only to find the id, when a decent search page already exists.

Gabriel H

after many trial and error: shouldOverrideUrlLoading or onPageStarted never gets called.

you should use -

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the link to YouTube Channel from Video Page?

Show one video in webview by link from YouTube

How to smooth scroll to an id after clicking a link from another page

Retrieve video ID from YouTube link

How to get the link from blogger video by the video ID?

How do I get the YouTube video ID from a URL?

How to get youtube video id from URL with java?

How to get live video id from YouTube channel

How can I get a Youtube video ID from the Youtube API given the video url?

Get length of YouTube video from page source?

How to get video tags from youtube video?

Python youtube search how to get "link"?

How to search videos on youtube and get link with python

how to get youtube player id and stop video

How to get ID of last uploaded video to YouTube?

How to return the link to the first Youtube video after a search in Selenium Python?

Youtube Data API - How to get video comments from multiple video id

Get youtube video id from youtube url in smarty template?

Open page in WebView by clicking on an external link

Get youtube video Id from shared video Url

Get youtube video link in python

How to get the data of a video from youtube in javascript

Is it possible to get url of first YouTube video from search?

Flutter Dart : How to extract youtube video ID from video URL?

How do I get a YouTube video thumbnail from the YouTube API?

How to open a Youtube video link directly from android app?

How do I parse YouTube URLs to get the video ID from a URL using Dart?

I need some help in correcting my codes in php - How to get the video id from a YouTube URL

How to Get Video IDs from Youtube video feeds?