在应用程序的背景上关闭视频Android

问题描述:

我使用两个Activity制作应用程序android。 我的第二个活动包含一个YouTube视频嵌入。 当我启动此活动并运行此视频时,它可以正常工作。 但是,当我关闭我的活动时,如果我的视频正在播放,我的活动已关闭,但我听到了我的视频。 我认为我的视频并没有真正关闭,她在后台播放。在应用程序的背景上关闭视频Android

你能帮助我吗?

谢谢!

编辑:我在我的MyActivity中构建一个测试,并且遇到同样的问题。

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"); 

} 
+0

你如何关闭您的活动以下职位?你打电话给'finish()'还是在关闭时向活动提供任何命令? – Grambot 2013-04-04 13:57:35

+0

要关闭我的Activity,我使用onPause();的onStop();和onDestroy(); – floflo41 2013-04-04 14:03:08

+1

这些只是您在各个阶段控制活动生命周期的重载方法。看到这个链接,你必须调用'finish()'来销毁活动:http://developer.android.com/guide/components/activities.html#ShuttingDown – Grambot 2013-04-04 14:31:07

我发现这个,它的工作原理! 查看有关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) { 
     ... 
    } 
}