如何通过API“input type='file'”标签更改本地文件的视频源...没有Jquery或外部库

Pix*_*imp 3 html javascript video

我正在尝试通过 API“input type='file' ”标签使用本地文件更改 HTML5 视频源。此 HTML 脚本旨在仅在本地运行,播放我的高清视频,但我无法获取“输入”标签来选择和播放我的高清视频文件。 jsfiddle 示例

超文本标记语言

<!----------------HOW to cahnge VID source Localy--------------------->
<p>working example</p>
<div id="selectFile">
<p>Change VID:
    <br>

    <button onclick="changeVID()">Change VID</button>

</div>
<div>
<video muted controls  id="videoPlayer" >
        <source id='currentVID' src="http://html5multimedia.com/code/ch9/media/elephants-dream-medium.mp4" type="video/mp4">

    </video>
</div>
<!----------desired result--------------->
<br>
<div id="desired result" style="background-color:grey;">
<p>desired result</p>
<br>

<div id="selectFile">
<p>Change local VID:
    <br>
<input id="newlocalFILE" name="localfile" size="50" maxlength="100000"
    accept="text/*" type="file" onchange="playlocalVID();">

</div>
<div>
<video muted controls  id="videoPlayer" >
        <source id='currentVID' src="c:\LocalVID.mp4" type="video/mp4">

    </video>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

脚本语言

/***********working example****************/

function changeVID(){
        var player = document.getElementById("videoPlayer");
        var currentVID = document.getElementById('currentVID');
        currentVID.setAttribute('src', "http://media.w3.org/2010/05/sintel/trailer.mp4");
        player.load();
        player.play();
}


/***********desired result****************/


function CangelocalVID(){
        var player = document.getElementById("videoPlayer");
        var currentVID =  document.getElementById('currentVID');
        var selectedLocalVID = document.getElementById('newlocalFILE').value;
        currentVID.setAttribute('src',selectedLocalVID);
        player.load();
        player.play();
}
Run Code Online (Sandbox Code Playgroud)

gue*_*314 5

编辑、更新

playlocalVID替换为视频上传处理程序的名称CangelocalVIDdocument.getElementById('newlocalFILE').files[0]为了document.getElementById('newlocalFILE').value; 尝试使用来自Creative Commons - Science Commons的视频“Science Commons”;似乎从本地上传上传,在jsfiddle上播放视频


尝试将元素accept的属性调整为,利用对象创建来 设置属性inputvideo/*Blob FileobjectURLselectedLocalVID.files[0]srccurrentVID


function playlocalVID() {
  var player = document.getElementById("videoPlayer");
  var currentVID = document.getElementById("currentVID");
  var selectedLocalVID = document.getElementById("newlocalFILE").files[0];
  currentVID.setAttribute("src", URL.createObjectURL(selectedLocalVID));
  player.load();
  player.play();
}
Run Code Online (Sandbox Code Playgroud)
<div id="desired result" style="background-color:grey;">
  <p>desired result</p>
  <br>
  <div id="selectFile">
    <p>Change local VID:
      <br>
      <input id="newlocalFILE" name="localfile" size="50" maxlength="100000" accept="video/*" type="file" onchange="playlocalVID();">
  </div>
  <div>
    <video muted controls id="videoPlayer">
      <source id="currentVID" src="http://html5multimedia.com/code/ch9/media/elephants-dream-medium.mp4">
    </video>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

jsfiddle https://jsfiddle.net/q3hhk17e/12/