1

I am working on an App and YouTube.com is embedded into a WebView. Videos cannot be played when I click any ones (it is in loading status forever). So I try to use VideoView or YouTubePlayerFragment to play videos when user clicks video play button. My question is: Are there any ways to detect that user has clicked play button?

I added below listener on WebView, but the hr is insufficient to tell use clicks play button.

    webView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            WebView.HitTestResult hr = ((WebView)v).getHitTestResult();

            return false;
        }
    });
Huigege
  • 115
  • 1
  • 12
  • Did you find any solution lately>? – WideFide Jul 30 '16 at 08:19
  • @WideFide, I don't know the root cause but it turns out that youtube view cannot be played in webview because i set android:layerType="software" for webview in the layout. After I remove this from layout, videos can be played and paused. – Huigege Aug 04 '16 at 00:38
  • please check this :[my answer](https://stackoverflow.com/questions/10811748/html5-in-a-webview-detect-when-video-is-started/44397791#44397791) – saed Jun 06 '17 at 19:06

1 Answers1

-1

After investigation, I use the solution provided in other posts: WebView link click open default browser and Support for other protocols in Android webview

webView.setWebViewClient(new WebViewClient() {
    ...
    @Override
    public void onPageFinished(WebView view, String url) {
        String string = "YouTube URL regular expression";
        if (url.toLowerCase().matches(string)) {
            launchInBrowserOrYouTubeApp(url);
        }
    }
    ...
}

There is still one issue for this solution. When I click any videos for the first time, this method is triggered and the url is 'www.youtube.com' (the url is 'www.youtube.com/watch?v=ajsdfhjh' when I click any videos second times or more), which is exact the same when the webview is initialized. So I cannot tell if it is user who clicks a video or it is the webview initialization.

Community
  • 1
  • 1
Huigege
  • 115
  • 1
  • 12