Question

I have a function that plays music with soundmanager 2 and the function works when I press the button to load the next sound however when I try to do this via the onfinish function it doesn't work which means when the music is finish playing it doesn't load the next sound how can I get this to work please see code below.

function playSong(id) {
//Stop the sound from playing
soundManager.destroySound(mySound);

//If we're under, just go to the end!
if (id < 0)
    id = songs.length - 1;

//If we're over, choose what to do via the repeat flag
if (id >= songs.length)
        id = 0;

//Save some variables
playingSongId = id;
playingSong = songs[playingSongId];
track = playingSong.artist_name + " " + "-" + " " + playingSong.track_name;

//Create the sound and begin playing whenever!
soundManager.createSound({
    id: mySound,
    url: playingSong.track_url,
    multiShotEvents: true,
    autoPlay: true,
    stream: true,
    onplay: function () {
        setPlayingInfo(track);
        setPauseState(false);
        setPlayTime();

    },
    onfinish: function() {
        playNextSong();
        /*//We'll only continue if we're shuffling or repeating if we past the end...
        if (shuffle == false && (playingSongId + 1 >= songs.length) && repeat == false) {
            //No more songs...
            setPlayingInfo();
            setPauseState(false);
            setPlayTime();
            playingSong = null;
            playingSongId = null;
            return;
        }
        else {
            playNextSong();
        }*/
    },

The function works as the title of the next sound appears in the header but the song never plays. Lastly when I click on the first sound link while the sound loads and plays the title doesn't load however if I click on the button to load the next sound the title appears. Note that I am loading these sounds via ajax as the sounds are on a remote server.

Was it helpful?

Solution

From SoundManager2's Revision History:

Flash Player 11.6.602.171, released by Adobe on 02/26/2013, introduced an issue with SM2's default Flash 8 (flashVersion: 8) API-based JS/Flash interaction, where SM2 methods called from callbacks such as onfinish() would not work. This primarily broke methods used for playing sounds in sequence, serially loading a series of sounds and so on. (See discussion for more.)

Note that this does not affect cases where soundManager.setup({ flashVersion: 9}) is being used; however, SM2 does use flashVersion: 8 by default.

Specifically, Flash-initiated events (such as a sound finishing) make Flash -> JS calls to the SM2 API, which subsequently call user-specified event handlers. If the user-specified SM2 onfinish() handler immediately calls a SM2 method like play() that makes a JS -> Flash call, this call either silently fails or is blocked. Other JS + Flash libraries that use similar callback patterns may also be affected, if their SWF is built targeting the Flash 8 API.

Suspecting a timing or recursion/stack issue, it was found that introducing a setTimeout(callback, 0) to user-specified SM2 callbacks like onfinish() restored sequential/playlist functionality.

Flash Player 11.6.602.180, relased by Adobe on 3/12/2013, exhibits the same behaviour. To avoid additional hacks, SM2 applies this to all Flash 8-based API callbacks regardless of what version of Flash Player is installed. No regressions are anticipated as a result of this change.

Alternately, this issue can be avoided by using soundManager.setup({ flashVersion: 9 }) as the Flash 9-based API does not appear to have this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top