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

时间:2018-01-14 11:27:49

标签: javascript php autoplay

我曾经有人要求制作一个显示个人资料视频的简单仪表板。视频的位置保存在数据库中,因此我需要先检索它。 这是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>

这是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);
?>

问题是视频无法播放,在视频屏幕中我收到消息

  

不支持视频格式或MIME类型

并在浏览器控制台中收到此错误消息:

  

HTTP加载失败,状态为404.加载媒体资源http://localhost/video_autoplay/undefined failed.

但是当我尝试在控制台中调用vid_list[1];之类的值时,该值就存在,这意味着readyState = 4state = 200

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

完成后,我将代码更改为:

<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>