douqiang5809 2014-03-08 19:53
浏览 85
已采纳

JW Player根据已用时间执行操作

I have a video and series of comments on the video. Each comment has a specific time associated with the video. For example video 1 has 3 comments. one in second 15, another in second 30, and the last comment is in second 45.

I can show all the comments. However, instead I want each comment to be shown on its associated time of the video. For example, comment 1 should only be appeared in second 15 and lasts until second 30 where it is replaced by second comment and so on.

I'm using JW Player to play the video and can get getPosition() function to get the current elapsed time of the video. It should be a simple JavaScript code to achieve this but unfortunately I'm so beginner with JS.

My own idea to achieve this is to use onTime function and for each position check if there is a comment in the server or not, and retrieve if there is. As in:

<script>
      jwplayer("container1").onTime(
           function(event) { 
            {
               var comment = get_comment_of(event.position);
               setText(comment);
               function setText(text) {
                   document.getElementById("message").innerHTML = text;
               }

            });
</script>

However, this is an expensive function and will send huge number of requests to the server.

  • 写回答

1条回答 默认 最新

  • doufei4418 2014-03-16 00:16
    关注

    Ok, the best solution I have preloaded the comments in a javascript array when the page is loaded, then show them when they occur using onTime function. This way there is no need to send a new request on on every onTime frame. As the comments are already available on the client side

    var timeArray = <?php echo json_encode($timeArray) ?>;
    var commentArray = <?php echo json_encode($commentArray) ?>;
    jwplayer("container1").onTime(
        function(event) {
           for (var i = 0; i < timeArray.length; i++)
           {
               var time = timeArray[i]
               var comment = commentArray[i];
               if (event.position >= time) {
                   setText(comment);
               }
           }
    
    
           function setText(text) {
               document.getElementById("message").innerHTML = text;                     }
           }
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮