音频播放器JavaScript

时间:2018-07-13 01:22:08

标签: javascript html5 html5-audio audio-player

我正在尝试构建音频播放器。我似乎无法获得加载功能。我的歌曲在E:\Music Player\Songs文件目录路径中。

<script type=text/javascript>
  var songs = ["Nobody Compares To You - Gryffin.mp3", "Crawl Outta Love - Illenium.mp3",
    "Higher Ground - Odesza.mp3"
  ];
  var poster = ["Nobody Compares to You - Gryffin.png", "Awake - Illenium.jpg",
    "A Moment Apart  - Odesza.jpg"
  ];

  var songTitle = document.getElementById("songTitle");
  var fillBar = document.getElementById("fill");

  var song = new Audio();
  var currentSong = 0; // it points to the current song

  window.onload = playSong; // it will call the function playSong when window is load

  function playSong() {
    song.src = "./Songs/" + songs[currentSong]; //set the source of 0th song 
    songTitle.textContent = songs[currentSong]; // set the title of song
    song.play(); // play the song

  }
</script>

1 个答案:

答案 0 :(得分:0)

据我所知,window.onload = playSong;错误地调用了playSong函数。
window.onload = playSong(); //必须在函数名称后加上方括号。
试试这个。