gpt4 book ai didi

android - Youtube 在 WebView 中只播放一次

转载 作者:行者123 更新时间:2023-11-30 03:58:30 26 4
gpt4 key购买 nike

我有以下代码。它成功地从 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 failed

最佳答案

以下代码对我来说效果很好。尝试添加此
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);

关于android - Youtube 在 WebView 中只播放一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com