Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was able to play youtube video on WebView. I want to finish/close/destroy the WebView automatically when youtube video is completed.

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 was trying to play songs (audio/video) from the playlist when audio is there in playlist it played in my custom player and when youtube video is there is played in WebView and songs are playing one after another automatically onCompletion. Youtube video played in WebView but after the completion of video, webview still open (not going to be destroy/finish). How could I finish WebView and get back to previous activity?

like image 452
Hiren Dabhi Avatar asked Sep 21 '11 14:09

Hiren Dabhi


1 Answers

Android's webview implementation hides the underlying MediaPlayer related APIs and callbacks. The only situation when the application comes into picture is when a user tries to go full screen (clicking on full screen icon in video embedded in WebView). In this case, onShowCustomView() is called back provided the application has implemented and registered a WebChromeClient callback. There is no way for you to know when a video has finished playing inside a webview.

I know this isn't what you asked: but why not use the youtube app by dispatching an intent to play the url? You can use the startActivityForResult() method which will call back your onActivityFinished() API (you have to implement this callback) with a result code. Will this not work for you?

like image 168
vino Avatar answered Nov 19 '22 07:11

vino