change <audio> src with javascript

后端 未结 6 1570
离开以前
离开以前 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:17

    Try this:

    Replace:

    audio.load();
    

    with:

    audio.play();
    
    0 讨论(0)
  • 2020-11-27 05:20

    If you are storing metadata in a tag use data attributes eg.

    <li id="song1" data-value="song1.ogg"><button onclick="updateSource()">Item1</button></li>
    

    Now use the attribute to get the name of the song

    var audio = document.getElementById('audio');
    audio.src='audio/ogg/' + document.getElementById('song1').getAttribute('data-value');
    audio.load();
    
    0 讨论(0)
  • 2020-11-27 05:21

    with jQuery:

     $("#playerSource").attr("src", "new_src");
    
        var audio = $("#player");      
    
        audio[0].pause();
        audio[0].load();//suspends and restores all audio element
    
        if (isAutoplay) 
            audio[0].play();
    
    0 讨论(0)
  • 2020-11-27 05:23

    Here is how I did it using React and CJSX (Coffee JSX) based on Vitim.us solution. Using componentWillReceiveProps I was able to detect every property changes. Then I just check whether the url has changed between the future props and the current one. And voilà.

    @propTypes =
        element: React.PropTypes.shape({
             version: React.PropTypes.number
             params:
                 React.PropTypes.shape(
                     url: React.PropTypes.string.isRequired
                     filename: React.PropTypes.string.isRequired
                     title: React.PropTypes.string.isRequired
                     ext: React.PropTypes.string.isRequired
                 ).isRequired
         }).isRequired
    
    componentWillReceiveProps: (nextProps) ->
        element = ReactDOM.findDOMNode(this)
        audio = element.querySelector('audio')
        source = audio.querySelector('source')
    
        # When the url changes, we refresh the component manually so it reloads the loaded file
        if nextProps.element.params?.filename? and
        nextProps.element.params.url isnt @props.element.params.url
            source.src = nextProps.element.params.url
            audio.load()
    

    I had to do it this way, because even a change of state or a force redraw didn't work.

    0 讨论(0)
  • 2020-11-27 05:30

    change this

    audio.src='audio/ogg/' + document.getElementById(song1.ogg);
    

    to

    audio.src='audio/ogg/' + document.getElementById('song1');
    
    0 讨论(0)
  • 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
    };
    <ul style="list-style: none">
      <li>Audio Files
        <ul id="list">
          <li><a href="#" data-value="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga">Death_Becomes_Fur.oga</a></li>
          <li><a href="#" data-value="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.mp4">Death_Becomes_Fur.mp4</a></li>
          <li><a href="#" data-value="http://media.w3.org/2010/11/rrs006.oga">rrs006.oga</a></li>
          <li><a href="#" data-value="http://media.w3.org/2010/05/sound/sound_90.mp3">sound_90.mp3</a></li>
        </ul>
      </li>
    </ul>
    
    <audio id="audio" controls="controls">
      <source id="audioSource" src=""></source>
      Your browser does not support the audio format.
    </audio>

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

    0 讨论(0)
提交回复
热议问题