duanjing2013 2018-01-14 11:27
浏览 61
已采纳

自动播放从数据库中检索到的位置的视频

I asked by someone to make a simple dashboard that show profiles video. The location of the videos is save in database, so i need to retrieve it first. That's my code in index.php

<body onload="onload();">
    <video style="border: 5px solid black" controls="" id="video" width="720" height="480" onended="onVideoEnded();">
    </video>
    <script>
        var vid_list = [];
        var list_index = 0;
        var video_player = null;
        function onload(){
            console.log("body loaded");
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "get_location.php", true);
            xmlhttp.send();
            xmlhttp.onreadystatechange = function(){
                if (this.readyState == 4 && this.status == 200) {
                    var data = JSON.parse(this.responseText);
                    vid_list = [];
                    for(var i = 0; i < data.length; i++){
                        vid_list.push(data[i].lokasi);
                        console.log(vid_list[i]); //running
                    }
                }
            }
            for(var x = 0; x < vid_list.length; x++){
                console.log(vid_list[x]);
            }
            video_player = document.getElementById("video");
            video_player.setAttribute("src", vid_list[list_index]);
            video_player.play();
        }

        function onVideoEnded(){
            //console.log("video ended");
            if(list_index < vid_list.length - 1){
                list_index++;
            }
            else{
                list_index = 0;
            }
            video_player.setAttribute("src", vid_list[list_index]);
            video_player.play();
        }
</script>
</body>

and this is get_location.php

<?php
    $con = mysqli_connect("localhost","root","","video");

    $sql = "SELECT * FROM video";
    $result = mysqli_query($con,$sql);

    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
    echo json_encode($data);
?>

The problem is the video cannot be played, in the video screen i get message

Video Format or MIME type is not supported

and in browser console, a got this error message :

HTTP load failed with status 404. Load of media resource http://localhost/video_autoplay/undefined failed.

But when i try to call a value like vid_list[1]; in the console, the value is exist, thats mean readyState = 4 and state = 200

can somebody help me ?

  • 写回答

1条回答 默认 最新

  • dongyied24121 2018-01-16 05:16
    关注

    Done, I change the code to :

    <body>
        <video style="border: 5px solid black" controls="" id="video" width="720" height="480" onended="onVideoEnded();">
        </video>
    </body>
        <script>
            var vid_list = [];
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "get_location.php", true);
            xmlhttp.send();
            xmlhttp.onreadystatechange = function(){
                if (this.readyState == 4 && this.status == 200) {
                    var data = JSON.parse(this.responseText);
                    vid_list = [];
                    for(var i = 0; i < data.length; i++){
                        vid_list.push(data[i].lokasi);
                        console.log(vid_list[i]);//running
                    }
                    testing(vid_list);
                }
            }
            //console.log(vid_list);
            var list_index = 0;
            var video_player = null;
            function testing(param){
                console.log(param);
                // console.log("body loaded");
                video_player = document.getElementById("video");
                video_player.setAttribute("src", param[list_index]);
                console.log(list_index);
                console.log(param);
                video_player.play();
                video_player.volume = 0.30;
            }
    
            function onVideoEnded(){
                //console.log("video ended");
                if(list_index < vid_list.length - 1){
                    list_index++;
                }
                else{
                    list_index = 0;
                }
                video_player.setAttribute("src", vid_list[list_index]);
                video_player.play();
            }
            //document.getElementById("txtHint").innerHTML = vid_list[2];
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)