Try to add .play()
, other detail lack send()
in your Ajax, autoplay
does not seem necessary, just run play
as soon as the caption is started.
An important detail , this does not exist :
document.getElementById("video").remove();
The function .remove
does not exist in DOM
, you should have confused the jQuery lib with pure JS, this is what should have given the whole error, do everything with jQuery itself:
$(document).on('click', '.class', function()
{
var videoLink = 'videos/video.mkv';
var subtitle = 'videos/subtitle.ass';
var video = $("#video");
var videoContainer = $("#videoContainer");
//Remove o video anterior
videoContainer.html("");
var videoHTML = ['<video class="video" controls>',
'<source src="'+videoLink+'"/>',
'</video>'].join('');
var x = new XMLHttpRequest();
x.open('GET', subtitle, 1);
x.onreadystatechange = function()
{
if (x.readyState === 4)
{
if (x.status !== 200) {
alert("Erro ao requisitar:" + x.status);
return;
}
//Atualiza a variável do video com o novo video
video = $(videoHTML);
//Adiciona o video ao container
video.appendTo(videoContainer);
var c = video.get(0);
setTimeout(function () {
var ass = new ASS();
c.play();
//video.get(0) pega o video como DOM e passa para o ASS.init
ass.init(x.responseText, c);
}, 100);
}
};
x.send(null); //**FALTAVA ISTO**
});
A functional example
$(document).on('click', '.class', function()
{
var videoLink = 'https://ass.js.org/test.mp4';
var subtitle = 'https://raw.githubusercontent.com/Aegisub/Aegisub/master/docs/specs/ass-format-tests.ass';
var video = $("#video");
var videoContainer = $("#videoContainer");
//Remove o video anterior
videoContainer.html("");
var videoHTML = ['<video class="video" controls>',
'<source src="'+videoLink+'"/>',
'</video>'].join('');
var x = new XMLHttpRequest();
x.open('GET', subtitle, 1);
x.onreadystatechange = function()
{
if (x.readyState === 4)
{
if (x.status !== 200) {
alert("Erro ao requisitar:" + x.status);
return;
}
//Atualiza a variável do video com o novo video
video = $(videoHTML);
//Adiciona o video ao container
video.appendTo(videoContainer);
var c = video.get(0);
setTimeout(function () {
var ass = new ASS();
c.play();
//video.get(0) pega o video como DOM e passa para o ASS.init
ass.init(x.responseText, c);
}, 100);
}
};
x.send(null); //**FALTAVA ISTO**
});
.video {
width: 480px;
height: 320px;
}
<script src="//rawgit.com/weizhenye/ASS/master/dist/ass.min.js"></script>
<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<div id="videoContainer"></div>
<p>
<a class="class" href="#">Iniciar</a>
</p>
No Jsfiddle : link