How to get event on completion of youtube video in android webview

I was able to play youtube video on WebView . I want to end / close / destroy WebView automatically when the YouTube video ends.

here is my code:

  WebView engine = new WebView(this); engine.getSettings().setJavaScriptEnabled(true); engine.getSettings().setPluginsEnabled(true); engine.loadUrl("http://www.youtube.com/embed/bIPcobKMB94?autoplay=1&rel=0&loop=0");//&enablejsapi=1"); setContentView(engine); 

Actually, I tried to play songs (audio / video) from the playlist when the sound is in the playlist that it played in my user player, and when you watch the video in WebView format, and the songs play one after the other automatically at the end. Youtube videos play in the WebView , but after the video is completed, the WebView is still open (will not be destroyed / finished). How can I finish the WebView and return to the previous activity?

+6
source share
1 answer

The Android implementation of webview hides the core APIs and callbacks associated with MediaPlayer. The only situation when the application enters the picture is when the user tries to switch to full-screen mode (by clicking on the full-screen icon in the video embedded in WebView). In this case, onShowCustomView () is called back, provided that the application has implemented and registered the WebChromeClient callback. Unable to find out when the video finished playing in web browsing.

I know this is not what you asked for: but why not use the youtube app by sending the intention to play the URL? You can use the startActivityForResult () method, which will call back your onActivityFinished () API (you must implement this callback) with the result code. This will not work for you?

+1
source

Source: https://habr.com/ru/post/897753/


All Articles