简体   繁体   English

DOMException:加载失败,因为在 html 文件中找不到受支持的源

[英]DOMException: Failed to load because no supported source was found in html file

im getting DOMException: Failed to load because no supported source was found in audio.play().我收到 DOMException: Failed to load because no supported source was found in audio.play(). im getting his issue on audioElement.src = 'songs/${index+1}.mp3';我在 audioElement.src = 'songs/${index+1}.mp3' 上遇到了他的问题;

    console.log("Welcome to music player");
//initialize the Variables


let songIndex = 0;




 audioElement = new Audio("songs/1.mp3");


let masterPlay = document.getElementById('masterPlay');

let myProgressBar = document.getElementById('myProgressBar');

let gif = document.getElementById('gif');


let masterSongName = document.getElementById('masterSongName');

let songItem = Array.from(document.getElementsByClassName('songItem'));



 songs = [
    { songName: "closer - chainsmokers", filePath: "songs/1.mp3", coverPath: "1.jpg" },

    { songName: "a thousand years", filePath: "songs/2.mp3", coverPath: "2.jpg" },

    { songName: "Let me love you", filePath: "songs/3.mp3", coverPath: "3.jpg" },

    { songName: "someone you loved", filePath: "songs/4.mp3", coverPath: "4.jpg" },

    { songName: "Let me down slowly", filePath: "songs/5.mp3", coverPath: "1.jpg" },

    { songName: "stay - Justin beiber", filePath: "songs/6.mp3", coverPath: "1.jpg" },


]




  songItem.forEach((element,i) => {

   
      element.getElementsByTagName("img")[0].src=songs[i].coverPath;

      element.getElementsByClassName("songName")[0].innerText=songs[i].songName;
  
  })







masterPlay.addEventListener('click', () => {

    if (audioElement.paused || audioElement.currentTime <= 0) {

        audioElement.play();

        masterPlay.classList.remove('fa-play-circle')

        masterPlay.classList.add('fa-pause-circle')

        gif.style.opacity = 1;

    }

     else {
        audioElement.pause();

        masterPlay.classList.remove('fa-pause-circle');

        masterPlay.classList.add('fa-play-circle');

        gif.style.opacity = 0;
    }
})





audioElement.addEventListener('timeupdate',()=>{

    console.log('timeupdate'); 

      progress = parseInt((audioElement.currentTime/audioElement.duration)*100);

  
    myProgressBar.value=progress;
})

myProgressBar.addEventListener('change',()=>{

   audioElement.currentTime  = myProgressBar.value * audioElement.duration/100;
})

const makeAllPlays = ()=>{

    Array.from(document.getElementsByClassName('songItemPlay')).forEach((element)=>{

        element.classList.remove('fa-pause-circle');

        element.classList.add('fa-play-circle');
    })
}



Array.from(document.getElementsByClassName('songItemPlay')).forEach((element)=> {

    element.addEventListener('click',(e)=>{

        console.log(e.target);

        makeAllPlays();

      index = parseInt(e.target.id);

        e.target.classList.remove('fa-play-circle');

        e.target.classList.add('fa-pause-circle');

   audioElement.src = 'songs/${index+1}.mp3' ; // error generating on this line
   
   
   masterSongName.innerText=songs[songIndex].songName;

   audioElement.currentTime=0;

   audioElement.play();
   
   masterPlay.classList.remove('fa-pause-circle');

   masterPlay.classList.add('fa-play-circle');

 } )

})



document.getElementById('next').addEventListener('click',()=>{

if(songIndex>5){

    songIndex=0;

}

else{

    songIndex+=1;
}



audioElement.src = 'songs/${index}.mp3';

masterSongName.innerText=songs[songIndex].songName;

audioElement.currentTime=0;

audioElement.play();

masterPlay.classList.remove('fa-pause-circle');

masterPlay.classList.add('fa-play-circle');

})





document.getElementById('previous').addEventListener('click',()=>{

if(songIndex<=0){

    songIndex=0;
}
else{

    songIndex-=1;
}

audioElement.src = 'songs/${index}.mp3';

masterSongName.innerText=songs[songIndex].songName;

audioElement.currentTime=0;

audioElement.play();

masterPlay.classList.remove('fa-pause-circle');

masterPlay.classList.add('fa-play-circle');

})

template string needs backtick ` to work properly, you are using single quote '模板字符串需要反引号 ` 才能正常工作,您使用的是单引号 '

change改变

audioElement.src='songs/${songIndex+1}.mp3'

with

audioElement.src=`songs/${songIndex+1}.mp3` 

MDN docs MDN 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DOMException:加载失败,因为未找到支持的源 - DOMException: Failed to load because no supported source was found 无法加载音频 - DOMException:无法加载,因为找不到支持的源 - Cant load Audio - DOMException: Failed to load because no supported source was found Chrome:未捕获(承诺)DOMException:无法加载,因为找不到支持的源 - Chrome: Uncaught (in promise) DOMException: Failed to load because no supported source was found Android Chrome:未捕获(承诺)DOMException:由于找不到受支持的源而无法加载 - Android Chrome : Uncaught (in promise) DOMException: Failed to load because no supported source was found 在 Vue 中播放声音 onClick 事件返回 Uncaught (in promise) DOMException: Failed to load because no supported source was found - playing sound onClick event in Vue returns Uncaught (in promise) DOMException: Failed to load because no supported source was found 播放带有特殊字符的音频时,“(未承诺)DOMException:未能加载,因为找不到支持的源” - “Uncaught (in promise) DOMException: Failed to load because no supported source was found” when playing an audio with special characters 加载失败,因为找不到支持的源 - Failed to load because no supported source was found Phonegap视频“由于找不到支持的来源而无法加载” - Phonegap Video “Failed to load because no supported source was found” Android:URL.createObjectURL无法正常工作(由于找不到受支持的源而无法加载。) - Android: URL.createObjectURL does not work properly (Failed to load because no supported source was found.) Web worker importScripts DOMException 脚本加载失败 - Web worker importScripts DOMException script failed to load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM