How to add a song progress bar in SoundManager2?

2019-05-21 00:30发布

问题:

I am trying to add a progress bar to show what position a song is at during playback. It only needs to be a simple solution. I found some code online, which uses SoundManager2 http://www.schillmania.com/projects/soundmanager2/ features to adjust a div width to match the song's position but it doesn't seem to be working, not sure where I am going wrong, any help would be appreciated.

I have used all the required Soundmanager2 files and playback of the songs work. It's only the progress bar which is the issue.

here is the javascript:

soundManager.setup({
url: '/swf',
preferFlash: false,
waitForWindowLoad: true,

onready: function() {

var myTrack1 = soundManager.createSound({
id: 'trackOne', url: 'msamples/beep-1.mp3', volume: 80, autoPlay: false,});
$(".progBar").css('width', ((this.position/this.duration) * 100) + '%');  

$("li.a5 a").click(function(event){soundManager.play('trackOne');});
},

ontimeout: function() {
  // Uh-oh. SWF missing, Flash blocked or other issue
}
});

The HTML links use this format and the progress bar sits below:

<li class="a5">Song1:
<a href="msamples/beep-1.mp3" title="Play" class="sm2_button">Play</a>
<div id="track1"><div class="progBar"></div></div>
</li>

The CSS is:

#track1 {
background-color: #333333;
height: 10px;
margin-top: 10px;
position: relative;
width: 100%;
}

.progBar {
background-color:#FFF;
height:10px;
}

The idea is #track1 acts as the container for the progress bar and .progBar is the div which has it's width adjusted through the $(".progBar").css('width', ((this.position/this.duration) * 100) + '%'); code.

Thanks in advance for your help.

回答1:

The logic seems to be fine - what you need to is to call the modify width snippet while the track is playing, using the whileplaying method (I think):

soundManager.setup({
    url: '/swf',
    preferFlash: false,
    waitForWindowLoad: true,

    onready: function() {

        var myTrack1 = soundManager.createSound({
        id: 'trackOne', url: 'msamples/beep-1.mp3', volume: 80, autoPlay: false,});

        $("li.a5 a").click(function(event){soundManager.play('trackOne');});
    },

    whileplaying: function() {
      $(".progBar").css('width', ((this.position/this.duration) * 100) + '%');
    },

    ontimeout: function() {
      // Uh-oh. SWF missing, Flash blocked or other issue
    }
});


回答2:

got it to work by adding the whileplaying: under soundManager.createSound. I also added an onfinish: to reset the progress bar back to 0 width at the end.

  soundManager.createSound({
    id: 'mySound1',
    url: 'msamples/beep-1.mp3',
    whileplaying: function() {
    $(".progBar").css('width', ((this.position/this.duration) * 100) + '%');
        },
    onfinish: function() {
        $(".progBar").css('width', '0');
        }
    });