将视频播放时间保存到db

问题描述 投票:-1回答:1

我需要在HTML页面中显示视频,并每10秒将已经播放的当前视频百分比保存到数据库中。我希望页面(客户端)将包含百分比的隐藏表单发送给必须将其写入db的服务器。如果发布到同一页面,视频会重新启动,所以我想将表格发送到空白帧。视频运行时,该百分比未写入db。仅当我按下“ ExitFromVideo”提交按钮时数据库才写入,但百分比始终为0我在做什么错?

为了执行此操作,我将此代码放在php页面中:

...
<center>
      <video id="video" width="320" height="240" preload="auto" controls autoplay>
       <source src="video/video_0_0_0.mp4" type="video/mp4">
       Your browser does not support the video tag.
      </video>
      <p><input style="text-align:center;" type="submit" class="button-three" name="ExitFromVideo" value="Exit From Video"/>

      <iframe name="votar" style="display:none;"></iframe>
      <form id="form1" method="get" action="about:blank" target="votar">
        <input id="str_s2" name="percentage" type="hidden">
      </form>
    </center>
    ...

    <script>
    // Update the count down every x second
    (function () { function checkTime(i) {  return (i < 10) ? "0" + i : i; }
        function startTime() {
            //document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
            t = setTimeout(function () { startTime() }, 5000);
            myFunction();
            document.forms["form1"].submit();
        }
        startTime();
    })();

    function myFunction()
    {
      var x = document.getElementById("video");
      var z = "";
      if(x != null ) {z = parseInt( x.length(0) / 100 * x.played.end(0), 10); }
      else {z="null";}
      document.getElementById("str_s2").value = z;
      //From here the client must (by post) ask to server to save percentage already seen
    }
    </script>

包含在捕获帖子消息的php页面中:

if(isset($_POST['percentage']))
{
  $Percentage = intval($_POST['percentage']); 

  $userid = $_SESSION['userid'];
  $videotoplay = $_SESSION['videotoplay'];
  $ArgumentSelected = $_SESSION['ArgumentSelected'];
  $query = "SELECT * FROM secure_login.UserDataVideo WHERE UserId = $userid AND Video = $videotoplay AND Argument = $ArgumentSelected AND Profile = 0 limit 1";
  $results = mysqli_query($db, $query);
  if (mysqli_num_rows($results) == 1)
  {
   $query = "UPDATE secure_login.UserDataVideo SET Percentage = $Percentage  ModifiedTs = NOW()
   WHERE UserId = $userid AND Video = $Video AND Argument = $ArgumentSelected AND Profile = 0 LIMIT 1";
   $results = mysqli_query($db, $query);
  }
  else
  {
    $query = "INSERT INTO secure_login.UserDataVideo(UserId, Video, Argument, Profile, Percentage, CreationsTs, ModifiedTs)
    VALUES ($userid, $videotoplay, $ArgumentSelected, '0', $Percentage, now(), now() )";
    mysqli_query($db, $query);
  }
}
javascript php html video
1个回答
0
投票

我终于使用了此代码,它可以正常工作。

<script>
// Update the count down every 1 second only in video view
var x = setInterval(function()
{
  var timerss = document.getElementById("timerx");
  var video = document.getElementById("video");
  if(video != null)
  {
    timerss.textContent = Math.round(video.currentTime / video.duration * 100) + "%";
    var Percentage = Math.round(video.currentTime / video.duration * 100);
    var xhr = new XMLHttpRequest();
    xhr.open("POST", 'server.php', true);

    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function()
    { // Call a function when the state changes.
        if (xhr.readyState === XMLHttpRequest.DONE && this.status === 200)
        {
            // Request finished. Do processing here.
            //alert(xhr.responseText);
        }
    }
    xhr.send("writepercentage=" + Percentage +"&currenttime=" + video.currentTime);
  }
}, 1000);
</script>

php服务器页面:

if(isset($_POST['writepercentage']) && isset($_POST['currenttime']))
{
 $Percentage = $_SESSION['percentage'] = intval($_POST['writepercentage']);
 $query = "SELECT * FROM UserDataVideo WHERE UserId = {$_SESSION['userid']} AND Video = {$_SESSION['quiztoshow']} AND Argument = {$_SESSION['ArgumentSelected']} AND Profile = {$_SESSION['profile']} limit 1";
 $results = mysqli_query($dbuserdata, $query);
 $currenttime = intval($_POST['currenttime']);
 if (mysqli_num_rows($results) != false)
 {
  $query = "UPDATE UserDataVideo SET Percentage = $Percentage, LastSecondPlayed = $currenttime, ModifiedTs = now()
  WHERE UserId = {$_SESSION['userid']} AND Video = {$_SESSION['quiztoshow']} AND Argument = {$_SESSION['ArgumentSelected']} AND Profile = {$_SESSION['profile']} limit 1";
  $results = mysqli_query($dbuserdata, $query);
 }
 else
 {
  $query = "INSERT INTO UserDataVideo(UserId, Video, Argument, Profile, Percentage, LastSecondPlayed, CreationsTs, ModifiedTs)
  VALUES ({$_SESSION['userid']}, {$_SESSION['quiztoshow']}, {$_SESSION['ArgumentSelected']}, {$_SESSION['profile']}, $Percentage, $currenttime, now(), now() )";
  mysqli_query($dbuserdata, $query);
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.