本文介绍了无法使用 <video> 加载 YouTube 视频在文本字段中输入 youtube url 的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个模态窗口,它允许用户1.) 粘贴视频网址2.) 从本地驱动器中选择一个文件,然后使用 html5 的 标签进行预览.

选项 2.) 在本地选择视频文件效果很好.但是,当我在输入文本字段上粘贴 youtube 视频 url 时,视频播放器显示加载指示器然后停止.您无法播放视频,并且似乎无法成功加载.

我检查了谷歌浏览器的检查器并收到此消息

跨域读取阻塞 (CORB) 阻止了跨域响应https://www.youtube.com/... 使用 MIME 类型 text/html.

我怀疑问题出在这一行.

$('#modalInput_pasteURL').on('input',function(e){var videoUrl = $('#modalInput_pasteURL').val().trim();$(".video").attr("src", videoUrl);});

我在研究时读到 必须用于包含或播放来自 url 的 youtube 视频.在这种情况下,我只想使用 标签,这样我就只有一个视频容器.

我错过了什么?有什么建议吗?

以下是代码片段.

HTML 文件

<input type="text" class="modal_inputbox" id="modalInput_pasteURL"/><br><label>选择视频</label><input type="file" class="modalbtn_browseFiles" id="modalBtn_choose_video_file" value="浏览视频" accept="video/*"/><br><label>视频预览</label><div class="modalContainer_filePreview" id="modalContainer_videoPreview"><视频控件类=视频"宽度=510"高度=200"></视频 >

JS 文件

$('#modalInput_pasteURL').on('input',function(e){var videoUrl = $('#modalInput_pasteURL').val().trim();$(".video").attr("src", videoUrl);});$("#modalBtn_choose_video_file").on('change',function(event){var fileInput = document.getElementById('modalBtn_choose_video_file');var fileUrl = window.URL.createObjectURL(fileInput.files[0]);$(".video").attr("src", fileUrl);});

谢谢.

解决方案

YouTube 视频可以通过使用

标记并加载 YouTube IFrame Player API.下面的Demo是对OP代码的修改.它可以播放标准视频或 YT 视频.Stack Snippet 无法在 SO 站点上播放 YouTube 视频,因此要对其进行正确测试,您可以将整个代码复制并粘贴到其他地方,或者转到此 Plunker.

注意:如果您想加载 YT API 以便以编程方式控制 YT 播放器,则会包含一些注释掉的代码.

Plunker

演示

Demo 中评论的详细信息

由于沙盒,YouTube 视频无法在 SO 上播放,请转到 Plunker一个功能齐全的演示

/* 如果你想对 YT 播放器进行编程控制,请取消注释||下一个块和下一个块之后的块*//*/加载 YouTube IFrame 播放器 APIvar tag = document.createElement('script');tag.src = 'https://www.youtube.com/iframe_api';var firstScriptTag = document.getElementsByTagName('script')[0];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);*///参考视频标签var vid = document.getElementById('vid');/*/声明 YT 播放器var ytv;//定义 YT 播放器函数 onYouTubeIframeAPIReady() {ytv = new YT.Player('ytv');}*///设置开关行为$('.switchBtn').on('click', function(e) {$('.switchBtn').removeClass('act');$(e.target).addClass('act');});//将输入事件委托给#pasteURL$('#pasteURL').on('input', function(e) {//获取url字符串var videoUrl = $('#pasteURL').val().trim();//如果标准视频"开关处于活动状态...如果 ($('.v').is('.act')) {//...启用对视频的控制...vid.controls = true;//...将 src 设置为 url 字符串...vid.src = videoUrl;//...重置 vid 以提示新的 url视频加载();//...除此以外...} 别的 {//...暂停视频...视频.暂停();//...重置视频...vid.currentTime = 0;//...移除视频控件...vid.controls = false;//...将 ytv src 设置为新的 url$('#ytv').attr('src', videoUrl);}});$("#file").on('change', function(event) {var fileInput = $('#file')[0];var fileUrl = window.URL.createObjectURL(fileInput.files[0]);$("#vid").attr("src", fileUrl).prop("controls", true);});
输入,标签 {显示:内联块;字体:继承;边距:10px 0 0 0;}输入[类型=文本] {宽度:350px;}.团体 {指针事件:无;不透明度:0.4;}.switchBtn {宽度:110px;高度:18px;边距:15px -2px 10px -2px;边框:3px 起始灰色;填充:2px 3px 0;光标:指针;}.switchBtn.act {背景:番茄;颜色:#ff}.switchBtn.act~.group {指针事件:自动;不透明度:1;}.switchBtn.v {边框左上角半径:12px;边框左下角半径:12px;文本对齐:右;}.switchBtn.y {边框右上角半径:12px;边框右下角半径:12px;}.转变 {显示:无}.switch:checked+* {高度:405px;过渡:1s;}
<头><!--<link rel="stylesheet" href="style.css">--><身体><label for='vidURL' class='switchBtn v'>标准视频</label><label for='ytvURL' class='switchBtn y'>You Tube Video</label><br><label>测试标准网址:</label><input type='text' readonly value='https://ia800209.us.archive.org/24/items/WildlifeSampleVideo/Wildlife.mp4'><br><label>测试 YT 网址:</label><input type='text' readonly value='https://www.youtube.com/embed/Ch5MEJk5ZCQ?enablejsapi=1'><br><label>在此处粘贴网址 </label><input type="text" id="pasteURL"/></fieldset><label>选择视频 </label><input type="file" id="file" value="Browse Video" accept="video/*"/><br><label>视频预览</label><figure class="filePreview"><input id='vidURL' type='radio' name='switch' class='switch'><video id="vid" width="100%" height="0"></video><input id='ytvURL' type='radio' name='switch' class='switch'><iframe id="ytv" type="text/html" width="100%" height="0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></图><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--<script src="script.js"></script>--></html>

I'm working on a modal window which allows user to either 1.) paste a video url or 2.) select a file from local drive then get a preview using html5's <video>tag.

Option 2.) Selecting a video file locally work perfectly. However, when I paste a youtube video url on input text field, the video player shows a loading indicator then stops. You can't play the video and doesn't seem to load successfully.

I checked Google Chrome's inspector and get this message

I suspect the problem is in this line.

$('#modalInput_pasteURL').on('input',function(e){
    var videoUrl = $('#modalInput_pasteURL').val().trim();
    $(".video").attr("src", videoUrl);
});

I read while doing research that <iframe> must be used to contain or play a youtube video from a url. In this case, I'd like to use just <video> tag so that I only have one video container.

What am I missing? Any suggestions?

Below is are the code snippets.

HTML file

<label>Paste Url here</label>
<input type="text" class="modal_inputbox" id="modalInput_pasteURL"/><br>

<label>Select Video</label>
<input type="file" class="modalbtn_browseFiles" id="modalBtn_choose_video_file"  value="Browse Video" accept="video/*"/><br>

<label>Video Preview</label>
<div class="modalContainer_filePreview" id="modalContainer_videoPreview">
<video controls class="video" width="510" height="200">
</video >
</div>

JS file

$('#modalInput_pasteURL').on('input',function(e){
    var videoUrl = $('#modalInput_pasteURL').val().trim();
    $(".video").attr("src", videoUrl);
});

$("#modalBtn_choose_video_file").on('change',function(event){
    var fileInput = document.getElementById('modalBtn_choose_video_file');
    var fileUrl = window.URL.createObjectURL(fileInput.files[0]);
    $(".video").attr("src", fileUrl);
});

Thank you.

解决方案

YouTube videos can be embedded into a webpage by either using a <div> or an <iframe> tag and loading YouTube IFrame Player API. The following Demo is a modification of the OP code. It can play either a standard video or a YT video. The Stack Snippet cannot play YouTube videos on SO site, so to properly test it, you either copy and paste the entire code elsewhere or go to this Plunker.

Note: There is some code commented out that is included should you want to load the YT API in order to control the YT player programmatically.


Plunker

Demo

Details commented in Demo

The YouTube video will not play on SO due to sandbox, go to Plunker for a fully functional Demo


/* If you want programattic control over the YT Player uncomment
|| the following block and the block after the next block
*/
/*/ Load YouTube IFrame Player API
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
*/

// Reference video tag
var vid = document.getElementById('vid');

/*/ Declare YT Player
var ytv;
// Define YT Player 
function onYouTubeIframeAPIReady() {
  ytv = new YT.Player('ytv');
}
*/

// Seup switch behavior
$('.switchBtn').on('click', function(e) {
  $('.switchBtn').removeClass('act');
  $(e.target).addClass('act');
});

// Delegate input event to #pasteURL
$('#pasteURL').on('input', function(e) {

  // Get the url string
  var videoUrl = $('#pasteURL').val().trim();
  // if the "Standard Video" switch is active...
  if ($('.v').is('.act')) {
    // ...enable controls on vid...
    vid.controls = true;
    // ...set src to url string...
    vid.src = videoUrl;
    // ...reset vid so it cues new url
    vid.load();
    // ...otherwise...
  } else {
    // ...pause vid...
    vid.pause();
    // ...reset vid...
    vid.currentTime = 0;
    // ...remove vid controls...
    vid.controls = false;
    // ...set ytv src to new url
    $('#ytv').attr('src', videoUrl);
  }
});

$("#file").on('change', function(event) {
  var fileInput = $('#file')[0];
  var fileUrl = window.URL.createObjectURL(fileInput.files[0]);
  $("#vid").attr("src", fileUrl).prop("controls", true);
});
input,
label {
  display: inline-block;
  font: inherit;
  margin: 10px 0 0 0;
}

input[type=text] {
  width: 350px;
}

.group {
  pointer-events: none;
  opacity: 0.4;
}

.switchBtn {
  width: 110px;
  height: 18px;
  margin: 15px -2px 10px -2px;
  border: 3px outset grey;
  padding: 2px 3px 0;
  cursor: pointer;
}

.switchBtn.act {
  background: tomato;
  color: #fff
}

.switchBtn.act~.group {
  pointer-events: auto;
  opacity: 1;
}

.switchBtn.v {
  border-top-left-radius: 12px;
  border-bottom-left-radius: 12px;
  text-align: right;
}

.switchBtn.y {
  border-top-right-radius: 12px;
  border-bottom-right-radius: 12px;
}

.switch {
  display: none
}

.switch:checked+* {
  height: 405px;
  transition: 1s;
}
<!DOCTYPE html>
<html>

<head>
  <!--<link rel="stylesheet" href="style.css">-->

</head>

<body>

  <label for='vidURL' class='switchBtn v'>Standard Video</label>

  <label for='ytvURL' class='switchBtn y'>You Tube Video</label>
  <br>
  <fieldset class='group'>

    <label>Test Std URL: </label>
    <input type='text' readonly value='https://ia800209.us.archive.org/24/items/WildlifeSampleVideo/Wildlife.mp4'>
    <br>
    <label>Test YT URL: </label>
    <input type='text' readonly value='https://www.youtube.com/embed/Ch5MEJk5ZCQ?enablejsapi=1'>
    <br>

    <label>Paste Url here </label>
    <input type="text" id="pasteURL" />

  </fieldset>

  <label>Select Video </label>
  <input type="file" id="file" value="Browse Video" accept="video/*" />
  <br>

  <label>Video Preview</label>
  <figure class="filePreview">

    <input id='vidURL' type='radio' name='switch' class='switch'>

    <video id="vid" width="100%" height="0"></video>

    <input id='ytvURL' type='radio' name='switch' class='switch'>

    <iframe id="ytv" type="text/html" width="100%" height="0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

  </figure>



  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <!--<script src="script.js"></script>-->
</body>

</html>

这篇关于无法使用 &lt;video&gt; 加载 YouTube 视频在文本字段中输入 youtube url 的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 23:12