Play random audio

  1. 4 months ago

    Hi,
    I want to have a button that clicking on it will play a random audio (either from the assets or directly from a file)
    I'm using a javascript , but I'm not sure how to how to actually play the audio.
    I'm trying something like this:
    var val = Math.floor(Math.random() * 8);
    if (val === 0) {
    $("#obj5117").trigger(PubCoder.Events.Run);
    }
    else if (val === 1) { ...

    But apparently the .trigger(PubCoder.Events.Run) doesn't play the audio.
    what am I missing?

  2. nothing? It can't be done?

  3. 3 months ago

    Angelo S

    Sep 2 Administrator

    Hello Ran,
    you can use:

    $("#obj5117")[0].play()

    or

    document.querySelector("#obj5117").play()

    which are equivalent

  4. Edited 3 months ago by Ran C

    Working perfectly, thanks!

  5. 2 months ago

    Angelo S Hello Ran,
    you can use:

    $("#obj5117")[0].play()

    or

    document.querySelector("#obj5117").play()

    which are equivalent

    Hello, do you have an step by step example or tutorial for how to implement the above solution. I have a few sound assets that I would like to play random on button press or action as well.

    Thank you kindly

  6. Select the button or ther interactive area that you want to trigger the random sound.
    Now, on the Interactivity panel on the right, add new action --> Run JavaScript
    Click the added action, and then the "Edit JavaScript" in the panel below.
    Enter your script in the window that opens.
    Here's an example of a random sound JS:

    var val = Math.floor(Math.random() * 5)
    if (val === 0) {
    $("#obj770")[0].play()
    }
    else if (val === 1) {
    $("#obj772")[0].play()
    }
    else if (val === 2) {
    $("#obj773")[0].play()
    }
    else if (val === 3) {
    $("#obj774")[0].play()
    }
    else {
    $("#obj775")[0].play()
    }

    How do you know the obj#, you ask? Easy: on the left of the JavaScript select Layers. You'll see all the objects in the page, with their object numbers. Simply use them instead of my examples.
    That's it.

  7. Hi Ran C, Thank you for taking the time. That did the trick for me. I am able to understand it now and implement it with your explanation.

    One thing I am encountering is that every so often in the preview window I will get a Javascript Error when press the interactive are for random sound:

    Script error.

    File:

    Line: 0

    not sure why that is the case. It happens at random times, but I do here the random sound files. Any ideas?I am just experimenting so I don't have a full project yet, just testing some ideas.

    Thanks again, I have learned a lot just by reading your posts.

  8. My guess is either one of the obj# is incorrect (and the software can't find it, hence the error) or the random value is out of range. Make sure the # of objects you want to play matches the value in Math.floor(Math.random())

  9. Fantastic. Working now. It was the random value being out range. Thanks again!

 

or Sign Up to reply!