如何通过html5音频元素从php播放音频文件

时间:2017-09-22 05:32:55

标签: javascript php html html5 yii2

我想流式传输位于应用程序服务器目录中的.wav音频文件,不应将文件名公开给最终用户。我顺便使用Yii2框架,一般来说,如何通过PHP实现这一目标?

3 个答案:

答案 0 :(得分:2)

简单的方法。如果您不想公开文件名,请不要使用名称属性。

<html>
<body>

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

</body>
</html>

src 是音频文件的路径, type 是音频的类型。

答案 1 :(得分:0)

<?php
    foreach($video as $video) { ?>
       <video width="100%" height="390" controls="">
           <source src="<?php echo $video['video'];?>" type="video/mp4">
       </video>
<?php } ?>

答案 2 :(得分:0)

实际上我已经设法找到解决问题的方法。

控制器代码

    $audioFilePath = "audio_file_absolute_path.wav"

    header('Cache-Control: no-cache');
    header('Content-Transfer-Encoding: binary');
    header('Content-Type: audio/wav'); // sets the output content type to wav
    header('Content-Length: ' . filesize($audioFilePath)); // sets the legth of the file (in order to the HTML5 player to access the audio duration)
    header('Accept-Ranges: bytes');
    header('Content-Disposition: inline; filename="test_audio.wav"'); // set an output file name

    readfile($audioFilePath); // reads the file 

查看代码(音频播放器)

          <audio controls>
               <source id="audioPlayerSource" src="<? 'Url of audio file read controller function' ?>" type="audio/wav">
          </audio>

这样,服务器中的音频文件将流式传输到视图中的音频播放器