我想流式传输位于应用程序服务器目录中的.wav音频文件,不应将文件名公开给最终用户。我顺便使用Yii2框架,一般来说,如何通过PHP实现这一目标?
答案 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>
这样,服务器中的音频文件将流式传输到视图中的音频播放器