0

I making an application android with two Activity. My second Activity contains an youtube video embed. When I launch this activity and run this video it works. But when I close my activity, if my video is playing, my activity is closed but I hear the sond of my video. I think my video is not really closed and she is playing in background.

can you help me?

thank !

edit : I build a test in my MyActivity and I have the same problem.

public class MyActivity extends Activity implements MBAdListener{

private FrameLayout layout;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RelativeLayout adViewContainer = (RelativeLayout) findViewById(R.id.adView);
    layout = new FrameLayout(this);

    layout.setLayoutParams(new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            Gravity.CENTER)
    );
    layout.setBackgroundColor(Color.BLUE);
    setContentView(layout);
    WebView content = new WebView(this);
    content.setLayoutParams(new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            Gravity.CENTER
    ));
    content.setHorizontalScrollBarEnabled(false);
    content.setVerticalScrollBarEnabled(false);
    content.getSettings().setJavaScriptEnabled(true);
    content.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    content.getSettings().setBuiltInZoomControls(false);
    content.getSettings().setSupportZoom(true);
    content.getSettings().setUseWideViewPort(true);
    content.getSettings().setPluginsEnabled(true);
    content.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    content.getSettings().setSavePassword(true);
    content.getSettings().setSaveFormData(true);
    content.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    layout.addView(content);

    content.loadData("<iframe width=\"320\" height=\"200\" src=\"http://www.youtube.com/embed/vCXRt0kQvUE?autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>", "text/html", "UTF-8");

}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
floflo41
  • 75
  • 3
  • 13
  • How are you closing your Activities? Do you call `finish()` or ever provide any commands to the activity while closing? – Grambot Apr 04 '13 at 13:57
  • To close my Activity, I use onPause(); onStop(); and onDestroy(); – floflo41 Apr 04 '13 at 14:03
  • 1
    Those are just methods you overload to have control over the activity lifecycle at the various stages. See this link, you'll have to call `finish()` in order to destroy the activity: http://developer.android.com/guide/components/activities.html#ShuttingDown – Grambot Apr 04 '13 at 14:31
  • I test with this : `@Override public void finish() { Logger.e("onFinish"); super.finish(); active = false; }` It doesn't work ! – floflo41 Apr 05 '13 at 08:20
  • You're overriding `finish()` and are better off *calling* `finish()` from your `onStop()` method. If you want to print a log message override `onFinish()` – Grambot Apr 05 '13 at 13:45
  • I test this also. I test another thing. I add my webview with my video embed in autoplay, when I click on closeButton, I call `layout.removeAllViews();` , my view is remove but not my video – floflo41 Apr 05 '13 at 14:00

1 Answers1

1

I found this and it works !!!! See the following post about How to stop youtube video playing in Android webview?

@Override
public void onPause() {
  super.onPause();

  try {
    Class.forName("android.webkit.WebView")
      .getMethod("onPause", (Class[]) null)
      .invoke(webview, (Object[]) null);
  } catch(ClassNotFoundException cnfe) {
      ...
  } catch(NoSuchMethodException nsme) {
      ...
  } catch(InvocationTargetException ite) {
      ...
  } catch (IllegalAccessException iae) {
      ...
  }
}
Community
  • 1
  • 1
floflo41
  • 75
  • 3
  • 13