在Android WebView中通过iframe加载Youtube视频 [英] Loading Youtube video through iframe in Android webview

查看:385
本文介绍了在Android WebView中通过iframe加载Youtube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to load youtube video to Android webview using iframe

这是我的布局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"

运行此程序时,我没有得到任何结果,只是显示黑屏

when I run this I didn't get any result its just showing a black screen

我尝试了 .但这为我提供了.3gp Quality上的视频.但我需要来自YouTube的原始质量的视频.这就是为什么我使用iframe.

I tried this .but this provide me video on .3gp Quality . but I need the videos from youtube on original quality. That's why I am using iframe.

我尝试使用<object></object><video></video>而不是iframe的代码.但这并不能解决我的问题.

I try code using <object></object> and <video></video> instead of iframe. but it didn't solve my issue.

当我在模拟器上运行此代码时,它会显示

when I run this code on emulator it shows

按下播放按钮之前

在视频上按播放"按钮后

After Pressing Play button on video

我认为我们无法在模拟器上流式传输视频,因为它是虚拟设备

I think we cannot stream videos on emulator since it is a virtual device

但是当我在手机上运行它时,它甚至都没有显示出结果.

But when I run this on phone it's not even showing this result.

我尝试将带有文档的iframe附加到手机以及仿真器上

I try iframe with a document attach to it works fine on phone as well as emulator

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

所以请帮助我将视频加载到此框架.

So please help me to load videos to this frame.

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

(I run it on phone). What's the problem? also will iframe work on Android 2.1?

有人尝试过 Youtube Api 吗?

推荐答案

HTML5视频支持

为了在您的应用程序中支持嵌入式HTML5视频,您需要打开硬件加速,并设置WebChromeClient .

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient.

要获得全屏支持,需要onShowCustomView(View,WebChromeClient.CustomViewCallback)和onHideCustomView()的实现,而getVideoLoadingProgressView()是可选的.

For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required, getVideoLoadingProgressView() is optional.

这篇关于在Android WebView中通过iframe加载Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆