gpt4 book ai didi

android - 在 Android webview 中通过 iframe 加载 Youtube 视频

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:39:00 29 4
gpt4 key购买 nike

我想使用 iframe 将 youtube 视频加载到 Android webview

这是我的布局 Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:id="@+id/mainLayout">

<WebView
android:background="@android:color/white"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>

我的代码是:

public class WebTube extends Activity {

private WebView wv;

String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/WBYnk3zR0os"
+ "?fs=0\" frameborder=\"0\">\n"
+ "</iframe>";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

wv = (WebView)findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", html , "text/html", "UTF-8", "");

}
}

我还提供<uses-permission android:name="android.permission.INTERNET"/>

& android:hardwareAccelerated="true"

当我运行它时,我没有得到任何结果,它只是显示黑屏

我试过了 this .但这为我提供了关于 .3gp Quality 的视频.但我需要来自 youtube 的原始质量的视频。这就是我使用 iframe 的原因.

我尝试使用 <object></object> 编写代码和 <video></video>而不是 iframe .但这并没有解决我的问题。

当我在模拟器上运行这段代码时它显示

在按下播放按钮之前

iframe result on emulator

在视频上按下播放按钮后

iframe result on emulator on play button click

我认为我们不能在模拟器上播放视频,因为它是一个虚拟设备

但是当我在手机上运行它时,它甚至没有显示这个结果。

我尝试在 iframe 上附加一个文档,在手机和模拟器上都能正常工作

String customHtml = "<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' width='100%' height='100%' style='border: none;'></iframe>";

所以请帮我加载视频到这个框架。

(我在手机上运行)。有什么问题?iframe 也可以在 Android 2.1 上运行吗?

有人试过吗Youtube Api

最佳答案

我有完全自定义的 ifram 用于 youtube View

public class Act_VideoPlayer extends Activity {

WebView webView;
ProgressBar progressBar;
ImageView back_btn;

String video_url = "KK9bwTlAvgo", html = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.full_screen_youtube_video_screen);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

back_btn = (ImageView) findViewById(R.id.full_videoview_btn);
back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webView.loadData("", "text/html", "UTF-8");
finish();
}
});

webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);

if (video_url.equalsIgnoreCase("")) {
finish();
return;
}

WebSettings ws = webView.getSettings();
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
ws.setPluginState(WebSettings.PluginState.ON);
ws.setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.reload();

if (networkUtil.isConnectingToInternet(Act_VideoPlayer.this)) {
html = getHTML(video_url);
} else {
html = "" + getResources().getString(R.string.The_internet_connection_appears_to_be_offline);
CustomToast.animRedTextMethod(Act_VideoPlayer.this, getResources().getString(R.string.The_internet_connection_appears_to_be_offline));
}

webView.loadData(html, "text/html", "UTF-8");

WebClientClass webViewClient = new WebClientClass(progressBar);
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient = new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
}




@Override
protected void onDestroy() {
super.onDestroy();
try {
webView.loadData("", "text/html", "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onBackPressed() {
super.onBackPressed();
try {
webView.loadData("", "text/html", "UTF-8");
finish();
} catch (Exception e) {
e.printStackTrace();
}
}


public class WebClientClass extends WebViewClient {
ProgressBar ProgressBar = null;

WebClientClass(ProgressBar progressBar) {
ProgressBar = progressBar;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
ProgressBar.setVisibility(View.VISIBLE);
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
ProgressBar.setVisibility(View.GONE);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
LogShowHide.LogShowHideMethod("webview-click :", "" + url.toString());
view.loadUrl(getHTML(video_url));
return true;
}
}

public String getHTML(String videoId) {
String html = "<iframe class=\"youtube-player\" " + "style=\"border: 0; width: 100%; height: 96%;"
+ "padding:0px; margin:0px\" " + "id=\"ytplayer\" type=\"text/html\" "
+ "src=\"http://www.youtube.com/embed/" + videoId
+ "?&theme=dark&autohide=2&modestbranding=1&showinfo=0&autoplay=1\fs=0\" frameborder=\"0\" "
+ "allowfullscreen autobuffer " + "controls onclick=\"this.play()\">\n" + "</iframe>\n";
LogShowHide.LogShowHideMethod("video-id from html url= ", "" + html);
return html;
}

}

关于android - 在 Android webview 中通过 iframe 加载 Youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156411/

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