-1

My HTML code is not working for some reason. My HTML Consist's of:

<input type="file" id="upload" accept=".mp4,.webm, .mov"/><br>
<video id="video" controls>
  <source src="" id="srcvideo" />
</video>
<script>
var y = document.getElementById("audio");
function handleFiles(event) {
    var files = event.target.files;
    $("#srcvideo").attr("srcvideo", URL.createObjectURL(files[0]));
    document.getElementById("video").load();
    y.play();
}
</script>

but for some reason, this code does not display the video. I tried to use the same code as you would audio, but it wont work either. if you have any suggestions please respond back!
-Thanks, rxgkit

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="upload" accept=".mp4,.webm, .mov"/><br>
<video id="video" controls>
  <source src="" id="srcvideo" />
</video>
<script>
var y = document.getElementById("audio");
function handleFiles(event) {
    var files = event.target.files;
    $("#srcvideo").attr("srcvideo", URL.createObjectURL(files[0]));
    document.getElementById("video").load();
    y.play();
}
</script>
rxgkit
  • 1
  • 2

1 Answers1

1

You are doing a couple of things wrong, not linking handleFiles anywhere and pointing 'y' to 'audio' instead of 'video', but overall your code is almost fine. Look at my snippet working.

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="upload" accept=".mp4,.webm, .mov"/><br>
<video id="video" controls>
</video>
<script>
function handleFiles(event) {
    var y = document.getElementById("video");
    var file = event.target.files[0];
    y.src = URL.createObjectURL(file);
    y.load();
    y.play();
}
const inputFile = document.getElementById('upload')
inputFile.addEventListener('change', handleFiles);
</script>
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • I think the Asker needs `.src = (window.URL || window.webkitURL).createObjectURL(file);` but that's Javascript and I'm not sure of the JQuery equivalent... – VC.One Sep 27 '21 at 14:35