Frage

I have to include an audio file, which automatically plays and can be paused and played again by buttons. The pausing and playing works, however it seems, that the file is loaded twice and I can't find the problem. When I pause, the sound volume just goes down and if I press the play button, one "layer" continues playing, where I paused it, the other just keeps on playing... This is the code:

$(document).ready(function() {
  $('#play').hide();
  soundManager.setup({
    debugMode: false,
    onready: function () {
      soundManager.createSound({
        id: 'music',
        url: 'up/files/file.mp3',
        autoPlay: true,
        autoLoad: false
      })
    }
  });

  $('#play').bind('click', function() {
    var sound = soundManager.getSoundById('music');
    sound.play();
    $('#pause').show();
    $('#play').hide();
  });

  $('#pause').bind('click', function() {
    var sound = soundManager.getSoundById('music');
    sound.pause();
    $('#pause').hide();
    $('#play').show();
  });
});

Edit

As Alex Morrise said, this seems to be a bug in the soundmanager2.js file. I now fixed this by providing a path to the swf-files and setting the preferFlash option to true, like so:

soundManager.setup({
  url: 'path/to/swf-files',
  preferFlash: true,
  debugMode: false,
  onready: function () {
    soundManager.createSound({
      id: 'music',
      url: 'up/files/file.mp3',
      autoPlay: true,
      autoLoad: false
    })
  }
});
War es hilfreich?

Lösung

This appears to be a bug in SoundManager. The soundManager.createSound method runs a _setup_html5 method. If you've set autoPlay, that _setup_html5 method calls a load function, which loads the sound.

However, in the load function, it checks to see if your browser supports HTML5. If it does, it again calls _setup_html5, which calls load the second time.

Do you follow?

Here's a sampling of the code:

this._setup_html5 = function(oOptions) {
    //...
    if (instanceOptions.autoLoad || instanceOptions.autoPlay) {
        s.load();
    }
    //...
}

this.load = function(oOptions) {
    //...
    if (html5OK(instanceOptions)) {
        oSound = s._setup_html5(instanceOptions);
        //...
    }
    //...
}

Andere Tipps

I came across this issue recently.

I noticed the message from Soundmanager2 debug output:

Cloning Audio() for instance #

The SoundObject cloning was not really useful for my purposes.

I edited the soundmanager2.js file. Search for the output string:

Cloning Audio()

around line 2021:

if (s.instanceCount < 2) {

        // HTML5 single-instance case

        start_html5_timer();

        a = s._setup_html5();

        s.setPosition(s._iO.position);

        a.play();

      } /*else {

        // HTML5 multi-shot case

        sm2._wD(s.id + ': Cloning Audio() for instance #' + s.instanceCount + '...');

        audioClone = new Audio(s._iO.url);

        onended = function() {

        ...

Commenting out the entire else option, the html5 support of soundmanager2 makes use of only one SMSound object.

It still allows to autoplay a element by creating a new object by using again the method

soundManager.createSound()

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top