音频播放器JavaScript

问题描述 投票:0回答:1

我正在尝试构建一个音频播放器。我似乎无法让on load功能工作。我的歌曲是在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>
javascript html5 html5-audio audio-player
1个回答
0
投票

据我所知,window.onload = playSong;错误地调用了playSong函数。 window.onload = playSong(); //函数名后面的括号是必需的。 试试这个吧。

© www.soinside.com 2019 - 2024. All rights reserved.