Youtube在WebView中只播放一次

时间:2012-10-18 05:16:28

标签: android flash webview youtube

我有以下代码。它成功地从YouTube流式传输视频。但是当视频结束时。即使我退出并重新进入应用程序,它也无法再次播放。它总是显示加载页面,然后立即跳转到建议的视频。

public class HdPlayer extends CustomView {
Button share_btn;
DisplaySize mDisplaySize;
String youtubeLink = null;
String thumbnailLink, description, VideoId, viewCount, date = null;
String title = null;
WebView webview;
String duration = null;

DbAdapter db;

/** Called when the activity is first created. */
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hd_player);
    db = new DbAdapter(this);

    titlebar_title.setText("Playing");
    titlebar_icon.setImageResource(R.drawable.titlebar_play);
    Bundle bundle = this.getIntent().getExtras();
    youtubeLink = bundle.getString("youtubeLink");


    webview = (WebView) findViewById(R.id.web_view_player);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setPluginState(PluginState.ON);
    webview.getSettings().setPluginsEnabled(true);
    // webview.getSettings().setLoadWithOverviewMode(true);// completely zoomed out
    // webview.getSettings().setUseWideViewPort(true);
    webview.getTouchables();

    webview.setBackgroundColor(Color.parseColor("#00000000"));
    mDisplaySize = new DisplaySize(this);

    Display display = getWindowManager().getDefaultDisplay();
    int[] widthHeight = new int[2];
    widthHeight = mDisplaySize.getDisplaySize(display);
    int width = widthHeight[0];
    int height = widthHeight[1];
    Log.i("iframe", " before iframe");
    String emdLink = "http://www.youtube.com/embed/" + VideoId;
    Log.i("iframe", " before loadData, youtubeLink: " + emdLink);

    LayoutParams lp = new RelativeLayout.LayoutParams(width,
            (width / 4) * 3);
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    webview.setLayoutParams(lp);
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl(emdLink);


}

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

    try {
        Log.i("in onpause", "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) {
    }
    webview.clearCache(true);
    webview.clearHistory();
    webview.destroy();
}




}

报告错误:dequeueBuffer:SurfaceComposer :: createGraphicBuffer失败

1 个答案:

答案 0 :(得分:1)

以下代码适用于我。尝试添加此 webView.setWebViewClient(new WebViewClient());

webView.setWebChromeClient(new WebChromeClient(){

});
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getTouchables();
webView.setBackgroundColor(Color.parseColor("#00000000"));
String emdLink = "http://www.youtube.com/embed/" + "hPUGNCIozp0";
webView.loadUrl(emdLink);