change <audio> src with javascript

后端 未结 6 1590
离开以前
离开以前 2020-11-27 04:52

I have multiple audio files that I want to stream based on the user selects. How do I do that? This is what I have so far and it doesn\'t seem to work.

*UPDATE: Mad

6条回答
  •  一向
    一向 (楼主)
    2020-11-27 05:36

    Try this snippet

    list.onclick = function(e) {
      e.preventDefault();
    
      var elm = e.target;
      var audio = document.getElementById('audio');
    
      var source = document.getElementById('audioSource');
      source.src = elm.getAttribute('data-value');
    
      audio.load(); //call this to just preload the audio without playing
      audio.play(); //call this to play the song right away
    };
    
    
    

    JSFiddle http://jsfiddle.net/jm6ky/2/

提交回复
热议问题