嵌入YouTube视频里面一个Android应用程序 [英] Embed Youtube video inside an Android app

查看:400
本文介绍了嵌入YouTube视频里面一个Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的WebView显示嵌入的YouTube视频和工程Galaxcy S2(OS 2.3.5),并没有在Nexus S(OS 2.3.4),我得到的是白色画面无任何视频展示

I'm using a WebView for displaying embedded Youtube video and that works on Galaxcy S2 (OS 2.3.5) and doesn't on Nexus S (OS 2.3.4), all I get is white screen without any video display.

下面是我使用的code段,并在清单文件中的声明:

Here is the code snippet I'm using and the declarations in Manifest file:

private WebView wv;

private void setWebView()
{
wv = (WebView) findViewById(R.id.webView);

wv.setWebChromeClient(new WebChromeClient());

wv.getSettings().setPluginState(WebSettings.PluginState.ON);

wv.setWebViewClient(new WebViewClient()); 

wv.getSettings();

wv.setBackgroundColor(0x00000000);

wv.setKeepScreenOn(true);

wv.setHorizontalScrollBarEnabled(false);
wv.setVerticalScrollBarEnabled(false);

wv.getSettings().setBuiltInZoomControls(true);

final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();

wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

}


public String getHTML()
{

String html = "<html>"

    + "<head>"
 + "</head>"
 + "<body style=\"border: 0; padding: 0\">"
 + "<iframe "
 + "type=\"text/html\" "
 + "class=\"youtube-player\" "
 + "width= 100%\""
 + "\" "
 + "height= 95%\""
 + "\" "
 + "src=\"http://www.youtube.com/v/"
 + selected_video 
    + "?controls=0&showinfo=0&showsearch=0&modestbranding=0" +
 "&autoplay=1&fs=1&vq=hd720\" " + "frameborder=\"0\"></iframe>" 
    + "</body>"
    + "</html>";

 return html;
}

请注意:参数selected_video是视频(标识(VideoID))的散列

Note: the parameter "selected_video" is the hash of the video (VideoID).

在清单文件中的声明:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android
.
.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"  />

<application
   .
   .
    android:hardwareAccelerated="true"  >

    .
    .

请让我知道如果你认识到什么我应该改变我的code,或一个完整的code,可支持所有Android设备和操作系统用于显示嵌入式(在-APP)与YouTube视频帮助质量高。

Please let me know in case you recognizing anything I should change in my code, or help with a complete code which can support all Android devices and OS for displaying embedded (In-App) Youtube video with high quality.

更新: 注意,我在寻找的解决方案应该显示高分辨率的视频。我把它用VideoView类在不同的设备和操作系统的工作,但视频质量不够好。所以包括VideoView或web视图或任何其他方式的任何解决方案,只有当它使得要被显示高质量的YouTube视频将被接受。感谢所有的反应!

UPDATE: Pay attention, the solution I'm looking for should display high resolution video. I got it work on the different devices and OS using VideoView class but the video quality isn't good enough. So any solution including VideoView or WebView or any other way will be accepted only if it makes high quality YouTube video to be displayed. Thanks to all the responders!

推荐答案

有官方的YouTube的Andr​​oid播放器API 至极,你可以使用。这是一个比较复杂一点,但它是工作比使用Web客户其他解决方案更好。

there is an official YouTube Android Player API wich you can use. This is a bit more complicated but it is working better than other solutions using webclients.

首先,你必须注册在 Google的API控制台。这是完全免费的,直到你的应用程序获得超过25K的要求一个月(或类似的东西)。有链接下完整的ANF伟大的教程。我希望你能理解他们。如果不能,请教! :)

First you must register your app in Googles API Console. This is completely free until your app gets over 25k request a month (or something like that). There are complete anf great tutorials under the link. I hope you can understand them. If not, ask! :)

这篇关于嵌入YouTube视频里面一个Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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