Pause/continue music
jota

Greetings,


So i have this scene and it is playing a background music, when i click a options button, it plays another music and stop the the first one, when i'm fine with setting the options(ok button) i want to play the first music, in the exact same point where the music stopped, i tried using the following, but it doesn't work:

var t=120-wade.getSceneObject('timer').time;
wade.playAudioSegment('audio/audio_6.ogg',t,120);

Can you please give some directions here? thank you

All 10 Comments
Gio

Hi

It seems that there is a bug with wade.playAudioSegment. Your code above should have worked.

It will be fixed in the next release of WADE, but in the meantime you can add the following script to your scene to patch up the bug.

wade.playAudioSegment = function(file, start, end, callback)
{
    if (this.getLoadingStatus(this.getFullPathAndFileName(file)) != 'ok')
    {
        return -1;
    }

    start = start || 0;
    var audio = this.getAudio(this.getFullPathAndFileName(file));
    var audioContext = this.getWebAudioContext();
    if (audioContext)
    {
        var source = audioContext.createBufferSource();
        source.buffer = audio;
        source.connect(audioContext.destination);
        source.endEventFired = false;
        source.onended = function()
        {
            source.endEventFired = true;
            if (callback)
            {
                callback();
                callback = null;
            }
        };
        source.start(0, start);
        if (callback)
        {
            var checkIfFinished = function ()
            {
                if (source.playbackState != source.FINISHED_STATE)
                {
                    setTimeout(checkIfFinished, wade.c_timeStep);
                }
                else if (source.onended && !source.endEventFired)
                {
                    source.endEventFired = true;
                    source.onended();
                }
            };
            source.checkEnded = setTimeout(checkIfFinished, (source.buffer.duration - start) * 1000 + wade.c_timeStep);
        }
        end && source.stop(end);
        audioSources.push(source);
    }
    else
    {
        end = end || audio.duration;
        var endFunction = function()
        {
            if (this.currentTime >= end)
            {
                this.pause();
                this.ended = true;
                callback && callback();
            }
        };
        if (audio.alreadyPlayed && !audio.ended)
        {
            audio = new Audio(audio.src);
        }
        audio.addEventListener('timeupdate', endFunction, false);
        audio.alreadyPlayed = true;
        audio.play();
        audioSources.push(audio);
    }
    return audioSources.length-1;
};

 

jota

HI,

In the scene, I add a script, and put the code above: 

https://ibb.co/n0Zc5G

is this the correct way? what am i missing here? i'm  getting a script error...

 

Gio

What error do you get?

jota

whenever call playAudioSegment() the console displays Script error. (, 0:0)

Gio

I see... it looks like it cannot be patched that way then.

Can you try with this script that I have attached below?

 

jota

to bad, doesn't work :(

Gio

It works when I try it here... if you tell me what error you are getting we can try to sort it out

jota

I'm not getting any error, the sound simply doesn't play when i click bt_ok (onMouseDown)  

it's quite possible that I'm doing something wrong, but I can not understand why?

i'm sending you a sample of the project, can you please take a look at it and try to figure out what's wrong?

Gio

Ok, I fixed it now, for real :) It's attached.

It seems that the web audio API has changed slightly since we wrote that code.

The difference is that I was calling it without specifying the "end" parameter, you were explicitly setting it to 120 (correctly), but this caused the sound to stop immediately due to another audio bug.

On a side note, you probably want to use the audio time for this, rather than a timer object which is ultimately based on the app's time. When there is a slow down for performance reasons (which is common when you load a scene with new assets and new textures for example), your wade app's time effectively slows down. An audio file that is playing on a different thread, in most cases, does not slow down.

You have 3 options really:

1. Assume that your users have a system that support WebAudio (i.e. no Internext Explorer, though Edge is fine). In this case you can do this extremely accurately using the web audio timer. When the sound start you do something like:
 

wade.app.soundStartTime = wade.getWebAudioContext().currentTime;

And later on, when you need to know how long ago the sound started playing:
 

var t = wade.getWebAudioContext().currentTime - wade.app.soundStartTime;

This is the most accurate method because it measures time in the same coordinate system as the sounds that are playing.

2. If you need to support internet explorer, you can assume (or hope) that the audio playback never stutters or slows down, in which case the audio timer is in sync with the system time.
 

wade.app.soundStartTime = Date.now() / 1000;
....
var t = Date.now() / 1000 - wade.app.soundStartTime;


3. Do both - if wade.isWebAudioSupported() use the first method, else use the second method.

On another side node, the game looks very good :)

jota

What can i say, thank you, thank you very much for all

it works just as i expected to 

keep up with the good job

Post a reply
Add Attachment
Submit Reply
Login to Reply