[英]HTML5 Video pause with Javascript when starting to play another video
我只是想知道是否有任何選項可以創建Javascript,以在您單擊另一視頻上的播放時暫停當前正在播放的視頻。 我在一頁上有大約30-40個HTML5視頻標簽。
我用來在頁面上放置視頻的代碼:
function pobierz_klipy($typ) {
$result = mysql_query("SELECT * FROM Klipy WHERE typ = $typ ORDER BY id")
or die("Zapytanie niepoprawne");
//ilosc zwroconych wierszy
$num_rows = mysql_num_rows($result);
$i = 0;
while ($i < $num_rows) {
$id = mysql_result($result, $i, "id");
$nazwa = mysql_result($result, $i, "nazwa");
$nazwa_rob = mysql_result($result, $i, "nazwa_rob");
$typ = mysql_result($result, $i, "typ");
$sprawdzian_hq = $sciezka = "../video/data/video/" . $nazwa_rob . "_fb.mp4";
if (file_exists($sprawdzian_hq)) {
$sciezka = "../video/data/video/" . $nazwa_rob . "_fb.mp4";
} else {
$sciezka = "../video/data/video/" . $nazwa_rob . ".mp4";
}
if (file_exists($sprawdzian_hq)) {
$poster = "../video/data/thumbnails/" . $nazwa_rob . "_fb.mp4.jpg";
} else {
$poster = "../video/data/thumbnails/" . $nazwa_rob . ".mp4.jpg";
}
if (file_exists($sprawdzian_hq)) {
$hq = "../video/data/video/" . $nazwa_rob . ".mp4";
} else {
$hq = null;
}
$i = $i + 1;
echo "<div class=\"klip\">";
echo '<video id="' . $nazwa_rob . '" width="320" height="180" controls poster= "' . $poster . '" preload ="none">';
echo ' <source id="source_' . $nazwa_rob . '" src="' . $sciezka . '" type="video/mp4">';
echo 'Twoja przeglądarka nie obsługuje HTML5.';
echo '</video>';
echo '<br />';
if ($hq != null) {
echo '<button id="videolink' . $i . '" type="button" class="btn btn-danger active" style=" font-size: 10px; padding: 5px; position: absolute; bottom: 2px; left: 11px;">HQ</button>';
}
echo '<div class="nazwa">'.$nazwa.'</div>';
//skrypt zmiany jakosci JS
if (file_exists($sprawdzian_hq)) {
echo '
<script type = "text/javascript">
var videobutton = document.getElementById("videolink' . $i . '");
videobutton.addEventListener("click", function (event) {
';
echo "
var videocontainer$i = document.getElementById('$nazwa_rob');
var videosource$i = document.getElementById('source_$nazwa_rob');
var newmp4$i = '$hq';
var old_mp4$i = '$sciezka';
var aktywne$i = 'btn btn-success active';
var nieaktywne$i = 'btn btn-danger active';
var przycikstatus$i = document.getElementById('videolink$i');
var obecny_klip$i = videosource$i.getAttribute('src');
var czas_klipu$i = videocontainer$i.currentTime;
console.log(czas_klipu$i);
if (obecny_klip$i != newmp4$i) {
var videosource$i = document.getElementById('source_$nazwa_rob');
videocontainer$i.pause();
videosource$i.setAttribute('src', newmp4$i);
videocontainer$i.load();
videocontainer$i.pause();
videocontainer$i.currentTime = czas_klipu$i;
przycikstatus$i.setAttribute('class', aktywne$i);
videocontainer$i.play();
} else {
var videosource$i = document . getElementById('source_$nazwa_rob');
videocontainer$i.pause();
videosource$i.setAttribute('src',old_mp4$i);
videocontainer$i.load();
videocontainer$i.pause();
videocontainer$i.currentTime = czas_klipu$i;
przycikstatus$i.setAttribute('class', nieaktywne$i);
videocontainer$i.play();
}
}, false);
</script>
";
}
您還可以使用此javascript代碼。
//external.js video = document.getElementsByClassName("video"); for (i = 0; i < video.length; i++) { video[i].onplay = function () { var currentIndex = index("video", this); for (k = 0; k < video.length; k++) { if (k == currenIndex) { continue } video[k].pause(); } } } function index(className, id) { nodes = document.getElementsByClassName(className); return [].indexOf.call(nodes, id); }
<html> <head> </head> <body> <video class="video" src="video1.mp4" controls></video> <video class="video" src="video2.mp4" controls></video> <video class="video" src="video3.mp4" controls></video> <video class="video" src="video4.mp4" controls></video> <script src="external.js"></script> </body> </html>
現在,此代碼還有另一個問題:
<script> video = document.getElementsByClassName("video");
for (i = 0; i < video.length; i++) {
video[i].onplay = function () {
var currentIndex = index("video", this);
for (k = 0; k < video.length; k++) {
if (k == currentIndex) { continue }
video[k].pause();
}
}
}
function index(className, id) {
nodes = document.getElementsByClassName("video");
return [].indexOf.call(nodes, id);
}
</script>
我在一頁上有大約30個視頻標簽。 當我嘗試依次播放6個視頻時,第7個視頻無法播放。
不好意思,我沒有收到通知。 我用他們相同的代碼連續播放了30個視頻,沒有問題。 嘗試看看您是否沒有將i值限制為7或類似的值。 您可能只有7個具有“視頻”類的標簽。 如果一切正常,請嘗試更換。 document.getElementsByClassName
通過document.getElementsByTagName
,然后再試一次。 很抱歉沒有及時回復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.