Why host your videos on Youtube?

为什么要在YouTube上托管您的视频?

Though, it is possible to get the video file URL of a YouTube video using sites like http://www.keepvid.com, but doing it programmatically is a problem you would like to give a pass. Hosting videos on your website involves a lot of challenges like making those videos work on multiple platforms and browsers, and making sure that video streaming doesn't take all your server's bandwidth due to the sheer size of videos. YouTube is already taking care of all those things, so it make sense to put all your videos on YouTube and let YouTube handle all these problems.

虽然可以使用http://www.keepvid.com之类的网站获取YouTube视频的视频文件URL,但是以编程方式进行操作是您想要传递的问题。 在您的网站上托管视频会遇到很多挑战,例如使这些视频可在多个平台和浏览器上运行,以及确保视频流不会占用视频服务器的全部带宽(由于视频数量庞大)。 YouTube已经在处理所有这些问题,因此将所有视频放到YouTube上并让YouTube处理所有这些问题是很有意义的。

Working code for Safari, Chrome, Iphone and Ipads

Safari,Chrome,Iphone和Ipad的工作代码

Fortunately, there is a way to embed a youtube video in your mobile app by writing the same code compatible to many browsers like Safari 4+, Chrome 6+, Iphone 3+, and Ipad 1 (iOS3+).

幸运的是,有一种方法可以通过编写与许多浏览器(例如Safari 4 +,Chrome 6 +,Iphone 3+和Ipad 1(iOS3 +))兼容的相同代码在移动应用程序中嵌入youtube视频。

<object id="idVideo" name="idVideo" width="90%" height="70%" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="http://www.youtube.com/watch?v=gCAcU3p9GNo&hl=en_US&fs=1&rel=0&hd=1&border=0&enablejsapi=1"/>';<param name="allowFullScreen" value="true"/>';<param name="allowScriptAccess" value="always"/>';<param name="wmode" value="transparent"/>';<embed src="http://www.youtube.com/watch?v=gCAcU3p9GNo&hl=en_US&fs=1&rel=0&hd=1&border=0&enablejsapi=1" type="application/x-shockwave-flash" allowScriptAccess="always" allowfullscreen="true" width="100%" id="idVideoPlayer" name="idVideoPlayer" wmode="transparent" height="80%"/>
</object>';

This markup should embed a youtube video on your website and has been tested successfully to work on browsers mentioned above.

此标记应在您的网站上嵌入youtube视频,并且已经过成功测试,可以在上述浏览器上使用。

Dynamic update of Youtube video URL

动态更新YouTube视频网址

Sometimes it makes sense to just update the video URL in a DHTML way rather than loading the Object element again. You can do so using the following JavaScript code

有时,仅以DHTML方式更新视频URL而不是再次加载Object元素是有意义的。 您可以使用以下JavaScript代码进行操作

//get the handle to Video object with id 'idVideo'
var videoObj = document.getElementById("idVideo");//get the handle to the array of param elements
var params = videoObj.getElementsByTagName("param");//iterate through the param elements
for ( var counter = 0; counter < params.length; counter++ )
{var param = params [ counter ];//if the param name is 'movie', then set the 'value' attribute to the youtube video URL you want to dynamically setif ( param.getAttribute( "name" ) == "movie" ){param.setAttribute( "value", "http://www.youtube.com/watch?v=gCAcU3p9GNo&hl=en_US&fs=1&rel=0&hd=1&border=0&enablejsapi=1" );}
}//finally set the same youtube video URL to the embed object with id 'idVideoPlayer'
document.getElementById( "idVideoPlayer" ).setAttribute( "src", "http://www.youtube.com/watch?v=gCAcU3p9GNo&hl=en_US&fs=1&rel=0&hd=1&border=0&enablejsapi=1" );

jQuery alternative for dynamically updating Youtube URL

jQuery替代品,用于动态更新Youtube URL

if you are using jquery, then your code would be reduced to

如果您使用的是jQuery,那么您的代码将减少为

$( "#idVideo param[name='movie']" ).attr("value", "http://www.youtube.com/watch?v=gCAcU3p9GNo&hl=en_US&fs=1&rel=0&hd=1&border=0&enablejsapi=1");
$( "#idVideoPlayer" ).attr( "src", "http://www.youtube.com/watch?v=gCAcU3p9GNo&hl=en_US&fs=1&rel=0&hd=1&border=0&enablejsapi=1");

Working code for browsers on Android phones

Android手机上浏览器的工作代码

BUT, the dynamic video content changing doesn't work on ANDROID phones. Iframes saves day for us here, and you can make it work on Android using the following code in that case

但是,动态视频内容更改不适用于ANDROID手机。 iframe在这里为我们节省了一天,在这种情况下,您可以使用以下代码使其在Android上运行

var videoURL = "http://www.youtube.com/embed/gCAcU3p9GNo";
var videohtml = '<iframe width="96%" height="75%" src="' + videoURL + '" class="youtube-player" frameborder="0"></iframe>';//iframe-video-container is the id of the element in which iframe will be set
$("#iframe-video-container").html(videohtml);

Other browsers

其它浏览器

Source code snippets mentioned above have been tested on older version of Firefox as well. It has not been tested on latest versions of Firefox, IE and Opera. However, since youtube works fine of all these browsers, so this code should work perfectly fine on them too.

上面提到的源代码片段也已经在较旧版本的Firefox上进行了测试。 尚未在Firefox,IE和Opera的最新版本上进行过测试。 但是,由于youtube在所有这些浏览器上都可以正常运行,因此此代码在它们上也应该可以正常运行。

翻译自: https://www.experts-exchange.com/articles/10080/How-to-embed-a-Youtube-video-on-your-mobile-and-desktop-web-application.html

如何在移动和桌面网络应用程序上嵌入YouTube视频?相关推荐

  1. 微信小程序上传大视频到七牛云-附七牛优惠码

    最近接到一个微信小程序的项目,客户要求将小程序视频上传到七牛云,并且由我们进行维护,大概看了下客户的需求,视频文件应该不少,每个月的费用自然也不少,小编在充值的时候突然看到有个优惠码选项,便在网上一搜 ...

  2. 如何在Plex上观看YouTube视频

    You've added all your media to a Plex server and it's great, but now you want to watch your favorite ...

  3. 如何在小程序上增加音视频?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 作者:常青|腾讯视频云终端技术负责人 2017年第四季度,腾讯云终端团队和微信合作,将腾讯云多年积 ...

  4. 如何设置计划任务程序 每6小时运行一次_如何用 Python 打造一个全自动赚钱的 YouTube 视频发布系统并月入过万(被动收益)

    前言 这篇文章将告诉你如何通过Python打造一台全自动发布YouTube视频并专区美元收益的系统. 目前我自己用的这套系统已基本稳定运行了三个月.其中一个YouTube频道从零起步到达到最低开通获利 ...

  5. adw -- android 平台上比较优秀的开源桌面应用程序之一

    转自 (源代码从这里下载) http://www.bangchui.org/read.php?tid=56 adw 是android平台上比较优秀的开源桌面应用程序之一. 类似苹果风格的  应用程序列 ...

  6. 桌面上每个图标点开都是计算机,我的电脑桌面图标程序都打不开怎么办?

    电脑的桌面图标双击打不开应该怎么办?小编今天就来教大家解决电脑桌面图标无法打开的问题. 1.有些小伙伴在使用电脑的时候偶尔会遇到桌面图标无法双击打开的问题,这个时候想要启动应用就很头疼了,今天小编就来 ...

  7. java 网站转app_java – 将现有Web应用程序转换为桌面应用程序

    我在这里看到了关于这个主题的一些其他类似问题,但是他们没有太大帮助所以我提出了这个问题. 将合理大小的简单Web应用程序转换为基于Java桌面的PC和Mac客户端的最快方法是什么?不幸的是,由于各种原 ...

  8. 在桌面应用程序与Web应用程序之间该如何选择

    今天开到一篇很好的文章,忍不住翻译过来.(版权归作者所有.如有侵权,请留言告知) 软件开发项目通常从一个常见的困境开始:桌面与 Web 应用程序.每个选项的优点和缺点使它成为一个艰难但至关重要的选择. ...

  9. php开发桌面应用程序_使用PHP开发跨平台桌面应用程序的3种方法

    php开发桌面应用程序 PHP as a cross-platform desktop app development language? Blasphemy! Nonetheless, it's p ...

最新文章

  1. oracle 修改用户密码_干货!数据库安全之Oracle数据库安全加固
  2. 腾讯,你妈喊你抄完作业回家吃饭
  3. You can't specify target table 'TS_AUTH_ADMIN' for update in FROM clause记录
  4. 疫情当前,企业“逆势而上”还需“上云”加速
  5. pion webrtc 示例代码解析 一
  6. Java程序运行时,没有赋值的基本类型变量会在内存中分配空间吗?
  7. vue-resource插件使用
  8. 最近用到的一个Debug类
  9. JavaScript 框架之战结束:React 是最终赢家?
  10. 2.mongoDB 命令
  11. reset.css(样式重置)
  12. php并发数据库操作,数据库的并发操作
  13. Java自学免费视频教程
  14. Linux_zlog日志系统的安装与使用
  15. Win10科大讯飞语音合成系统( Interphonic 5.0)安装方法及报错解决
  16. centos7安装杀毒软件ClamAV
  17. ARM Coresight
  18. Python3学习(33)--序列和反序列化(二)
  19. 安卓分屏模式的简单适配
  20. 警惕新型钓鱼邮件诈骗~伪装的“再次发送”邮件

热门文章

  1. Android高级控件
  2. java单个字符,字符串转数字
  3. 科普:小米松果澎湃S1的前世今生以及真实性能
  4. 网上订餐的发展现状及前景分析
  5. 有多少人在51job上找到工作_到底有没有人在51job上找到过工作?
  6. 网站源码下载项目,小白简单搬运月入1w+
  7. python四大主流web框架
  8. 桥接模式(Bridge Pattern)-(最通俗易懂的案例)
  9. Day1学习(计算机基础)
  10. QQ 微信化,微信 QQ 化?QQ 小程序内测页面曝光!