media-playermediayahooyahoo-widgets

Processing dynamic MP3 URL


I am using Yahoo Media Player for playing MP3 songs on my website.

I have put some static MP3 links on site. For e.g.

<a href='1.mp3'>Song1</a>
<a href='2.mp3'>Song2</a>

And I have put the YMP JavaScript API code also.

Now, I want to load the songs dynamically...

For example if user clicks on a button I want to load a complete new play list to the player.

//Something like this
var clickEventHandler = function(){
  YMP.removeAllSongs();
  YMP.addSongs(mp3_links);
  YMP.play();
}

Solution

  • This may help you, use this:

    <script>
    /** On Yahoo Media API Ready **/
    var yesReady = false;
    YAHOO.MediaPlayer.onAPIReady.subscribe(function(){
      yesReady = true;
    });
    
    function play(){
      //Capture the URL of the song
      var url = document.getElementById('url').value;
      //Put it in Href of Song Link
      document.getElementById('link').href = url;
    
      //After that Play the Song using YMP
      if(yesReady){
        YAHOO.MediaPlayer.addTracks(document.getElementById('song-div'), 0, true);
        YAHOO.MediaPlayer.play();
      }
    }
    </script>
    
    <div id='song-div'>
      <a href='#' id='link'>Song Name</a> 
    </div>
    
    <input type='text' id='url' />
    <input type='button' onClick="javascript:play('url')" value='play'/>
    

    Let me know if you need more help.