如何回显var在下一个代码中? (流网址)

时间:2018-04-03 15:18:19

标签: javascript php jquery html

首先我将url(php字符串)转换为jquery数组,

现在我需要使用var #url进行广播。

使用:

hls.loadSource('http://149.56.21.86:25461/live/demivadoiscar/Quz2LG/158.ts');

不起作用:

hls.loadSource('#url');

-

<?php
$url = "http://149.56.21.86:25461/live/demivadoiscar/Quz2LG/158.ts";
?>

<html>
<body>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<video height="600" id="video" controls></video>

var url = <?php $url?>

<script>
if(Hls.isSupported()){
    var video = document.getElementById('video');
    var hls = new Hls();
    hls.loadSource('#url');
    hls.attachMedia(video);
    hls.on(Hls.Events.MANIFEST_PARSED,function(){
        video.play();
    });
}
</script>

2 个答案:

答案 0 :(得分:3)

您需要将url作为字符串并在您的加载源函数中使用它,如下所示:

<script>
var url = '<?php echo $url; ?>';
//setup code
hls.loadSource(url);
//the rest of the code
</script>

答案 1 :(得分:-1)

试试这个:

<script>
    var url = "<?php echo $url; ?>";
    if(Hls.isSupported()){
       var video = document.getElementById('video');
       var hls = new Hls();
       hls.loadSource(url);
       hls.attachMedia(video);
       hls.on(Hls.Events.MANIFEST_PARSED,function(){
          video.play();
       });
    }
</script>