Code Monkey home page Code Monkey logo

Comments (8)

gavinbeck avatar gavinbeck commented on May 17, 2024 1

@goldfire "The play method already returns an ID"

How is that retrieved?

I am creating a looping sound, whose volume fades on scroll (based on a percentage of scrollTop and the distance from the "target"). However, it seems that each time I set the volume, it creates a new instance of the sound. I need to target just the existing instance and adjust only that.

from howler.js.

goldfire avatar goldfire commented on May 17, 2024

Well this gets a little tricky since the intended use is that you can play multiple instances of the same sound at the same time. However, this is tracked internally in sound._audioNode[x].paused. So, if you track the soundId of a specific playback, I could create a method that checks if that specific sound is playing or not. I could also make it so that if you don't pass a soundId, it'll loop through all audio nodes for that Howl object and return true if any of them are currently playing. Any thoughts on that?

from howler.js.

ndarilek avatar ndarilek commented on May 17, 2024

OK, I thought that might be the case. At the moment my design is an
entity system, and any entity that emits sound has a Sound component
with various properties (presence for a sound that plays at the entity's
position, various collision sounds, random creature sounds, etc.) I was
creating a separate Howl for each of these, and assigning that to the
entity so it could control its own audio.

This begs a more fundamental documentation/design question: should each
Howl allocate itself from a singleton pool keyed on URL? Then, if I
create various unique Howls with the same urls property, their creation
would simply be no-op and return the existing instance? play() could
then return the ID which I could then pass to isPlaying() to determine
if that instance is playing, or isPlaying() with no arguments could
check if any instance of the given Howl is playing?

This fleshes the subject matter of the issue out a bit, but it wasn't
obvious to me that one Howl was meant to be shared among many sound
instances. A reasonable compromise might be to make play() return an
instance ID if it doesn't already, then offer a method to determine if
the specified ID is playing.

from howler.js.

goldfire avatar goldfire commented on May 17, 2024

The play method already returns an ID, and you can use that ID to control pretty much everything about the sound. The reason that each Howl is designed to be able to play multiple instances of the same sound is becuase you wouldn't want to create many new Howl objects if you were for example shooting a gun in a game rapid-fire.

The play method accepts a callback as the 2nd parameter, and returns the ID in the callback (it has to be in a callback because the play method has to asynchronously wait for a sound to be ready to play before actually beginning playback).

from howler.js.

PaulMorris avatar PaulMorris commented on May 17, 2024

Hmmm... I wonder if using these instance IDs would offer a way to work around the IE9 problems I was running into? It doesn't look like you can use the instance ID to play that same instance more than once, but if that worked... then for IE9, in my code I could store the IDs and rotate through say 3 instances for a given sound. (That would be enough for my use case to keep clipping to a tolerable level.)

Background: the issues in IE9 seem to be related to the creation and destruction of too many audio instances, which IE9 can't handle without eventually dropping audio clips. So maybe the instance IDs could make it possible to only create a finite number of instances and then re-use them by rotating through them. (This is how I'm having to support IE9 outside of howler, and is the way I was doing it before I started using howler.)

But then if the instances were destroyed by howler, then they wouldn't be playable any more... (Is the pool of audio nodes/instances one big pool that all howls/sounds share together, or are there separate pools specific to each howl/sound?) So maybe there would need to be a way to set the size of the pool so that it would be big enough to hold the required (finite) number of instances so that none of them would get destroyed? If that makes sense?

Just thinking out loud here, this might be a can of worms you wouldn't want to open.

from howler.js.

ndarilek avatar ndarilek commented on May 17, 2024

Got it. Is there a function to determine if a sound with a given ID is
playing? I didn't see one but may have missed it.

Thanks.

from howler.js.

goldfire avatar goldfire commented on May 17, 2024

Sorry, somehow I never noticed there was a new comment here. You can get the ID like this:

sound.play(function(soundId){
    // do what you want with soundId
});

from howler.js.

Salitehkat avatar Salitehkat commented on May 17, 2024

Hi gavinbeck , 5 years later, did you manage to solve this? how did you prevent the creation of a new instance? I am facing the same problem. Many thanks

I am creating a looping sound, whose volume fades on scroll (based on a percentage of scrollTop and the distance from the "target"). However, it seems that each time I set the volume, it creates a new instance of the sound. I need to target just the existing instance and adjust only that.

from howler.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.