Javascript IOS:Can';t在Safari浏览器的音频文件上设置currentTime
我的问题是我无法在Safari上设置音频文件的当前时间。我尝试创建一个音频强大的文本流媒体平台,为此,我使用Symfony 5 在正面(细枝中),我有以下代码:Javascript IOS:Can';t在Safari浏览器的音频文件上设置currentTime,javascript,php,audio,safari,streaming,Javascript,Php,Audio,Safari,Streaming,我的问题是我无法在Safari上设置音频文件的当前时间。我尝试创建一个音频强大的文本流媒体平台,为此,我使用Symfony 5 在正面(细枝中),我有以下代码: {% set chapterPath= chapter.path|split('/') %} {% set chapterToken=(((chapterPath|last)|split('.'))|first) %} <audio id="chapterAudioFile
{% set chapterPath= chapter.path|split('/') %}
{% set chapterToken=(((chapterPath|last)|split('.'))|first) %}
<audio
id="chapterAudioFile"
style="visibility: hidden"
oncontextmenu="return false;"
controls
src="{{ path("app_chapter_play",{'id': chapter.id, 'chapterFileToken': chapterToken}) }}">
Your browser does not support the
<code>audio</code> element.
</audio>
为了得到我的音频文件,我请求它感谢我这样的路线:
/**
* @Route("/{id}/{chapterFileToken}", methods={"GET","POST"})
* @param Chapter $chapter
* @param string $chapterFileToken
* @return Response
*/
public function play(Chapter $chapter, string $chapterFileToken, Request $request)
{
$chapterPath=$chapter->getPath();
$chapterFilename=explode("/", $chapterPath);
$chapterToken=explode(".",end($chapterFilename))[0];
if ($chapterFileToken==$chapterToken){
$extension=explode(".", $chapterPath)[2];
if ($extension=="mp3") {
$mime_type = "audio/mpeg";
}
else{
$mime_type = "audio/ogg";
}
if(file_exists($chapterPath)){
$file=fopen($chapterPath,"r");
$content=fread($file,$filesize);
fclose($file);
$response = new StreamedResponse(
function () use ($content) {
echo ($content);
});
$response->headers->set('Content-Type', $mime_type);
$response->headers->set('Cache-Control', '');
$response->headers->set('Content-Length', strlen($content));
$response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s'));
$contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, end($chapterFilename));
$response->headers->set('Content-Disposition', $contentDisposition);
$response->prepare($request);
return $response;
}
else return new Response(Response::HTTP_NOT_FOUND);
}
else return new Response(Response::HTTP_FORBIDDEN);
}
问题是它在Firefox中工作,但在Safari中不起作用。当我尝试设置音频文件的currentTime时,它总是将其设置为0
Javascript函数:
var audio=document.getElementById('chapterAudioFile');
var audioDurationSlider=document.getElementById("audioFileDuration");
var playButton=document.getElementById("playButton");
var isPlaying=false;
audioDurationSlider.value=0;
audio.onloadedmetadata=()=>{
audioDurationSlider.max=audio.duration;
}
audioDurationSlider.onchange = () => audio.currentTime = audioDurationSlider.value;
audioDurationSlider.max=Math.round(audio.duration);
audio.onplay = () =>{
isPlaying=true;
playButton.innerHTML="<i class=\"fa fa-pause\"></i>";
}
audio.onpause= ()=>{
isPlaying=false;
playButton.innerHTML="<i class=\"fa fa-play\"></i>";
}
audio.ontimeupdate = () => {
audioDurationSlider.value = audio.currentTime;
document.getElementsByClassName("rangeCurrent")[0].innerHTML=getDuration(audio.currentTime)+" | "+getDuration(audio.duration);
}
function playAndPause(){
if (isPlaying){
pause();
}
else {
play();
}
}
function play(){
audio.play();
}
function pause(){
audio.pause();
}
function getDuration(time){
var minutes = Math.floor(time / 60);
var seconds = Math.round(time - minutes * 60);
var hours = Math.floor(time / 3600);
if (minutes/10<1) {
minutes = "0" + minutes
}
if (seconds/10<1) {
seconds = "0" + seconds;
}
if (hours===0){
return minutes+":"+seconds;
}
return hours+":"+minutes+":"+seconds;
}
function quickReturn(){
if (audio.currentTime-10>=0) {
audio.currentTime -= 10;
}
else audio.currentTime=0;
}
function quickAdvance(){
if (audio.currentTime+10<=audio.duration) {
audio.currentTime += 10;
}
else audio.currentTime=audio.duration;
}
function restart(){
audio.currentTime=0;
}
var audio=document.getElementById('chapterAudioFile');
var audioDurationSlider=document.getElementById(“audioFileDuration”);
var playButton=document.getElementById(“playButton”);
var显示=假;
audioDurationSlider.value=0;
audio.onloadedmetadata=()=>{
audioDurationSlider.max=audio.duration;
}
audioDurationSlider.onchange=()=>audio.currentTime=audioDurationSlider.value;
audioDurationSlider.max=Math.round(audio.duration);
audio.onplay=()=>{
isplay=true;
playButton.innerHTML=“”;
}
audio.onpause=()=>{
isplay=false;
playButton.innerHTML=“”;
}
audio.ontimeupdate=()=>{
audioDurationSlider.value=audio.currentTime;
document.getElementsByClassName(“rangeCurrent”)[0].innerHTML=getDuration(audio.currentTime)+“|”+getDuration(audio.duration);
}
函数playAndPause(){
如果(显示){
暂停();
}
否则{
play();
}
}
函数播放(){
音频播放();
}
函数暂停(){
audio.pause();
}
函数getDuration(时间){
var分钟=数学下限(时间/60);
var秒=数学轮(时间-分钟*60);
变量小时数=数学下限(时间/3600);
如果(10分钟)