Repeating sound from one Instance

拟墨画扇 提交于 2019-12-02 04:20:45

A SoundEffectInstance is one single instance of a sound playing; by definition, it cannot be played more than one at a time. Of course, you can have multiple instances of the same SoundEffect. In fact, this is basically what is happening when you call SoundEffect.Play(): you are creating an instance and playing it.

With this in mind, there are several options on your plate. If you [presumably] want to prevent a billion sounds from fully playing when you hold down a button, you definitely should not be using SoundEffect.Play(), as like you've discovered, many many instances will be created (this is potentially very bad for performance and overall management anyway). For lesser-frequent sounds that may be an okay method.

Having said that, in what way can the SoundEffectInstance be used? Basically it can be restarted from the beginning. Note that you can STILL have multiple SoundEffectInstances of course, one (or more) per game component. In this way one of your guns can restart and not mess with a different gun's instance of the same sound. Or you can choose to only have one instance per sound effect for performance reasons.

I've had problems with trying to restart it the "simple" way:

instance.Stop();
instance.Play();

Historically this has not worked for me; if it does for you, disregard the following:

What I had to do was set up my own "restart" flag per instance. So I wrapped the SoundEffectInstance class with my own like so:

//class that represents one sound effect instance PLUS restart flag
public class Sound
{
    SoundEffectInstance instance;
    bool restart;

    public Sound(SoundEffectInstance i)
    {
        instance = i;
        restart = false;
    }

    public bool PlayingOrRestarting()
    {
        return State == SoundState.Playing || restart;
    }

    public void Update()
    {
        if (restart)
        {
            instance.Play();
            restart = false;
        }
    }

    public void Play()
    {
        instance.Play();
    }

    public void Restart()
    {
        instance.Stop();
        restart = true;
    }

    public void Stop()
    {
        instance.Stop();
        restart = false;
    }

    public SoundState State
    {
        get { return instance.State; }
    }

    public float Volume
    {
        set { instance.Volume = value; }
        get { return instance.Volume; }
    }

    public bool IsLooped
    {
        set { instance.IsLooped = value; }
        get { return instance.IsLooped; }
    }
}

With this method you have to call Update() every frame for every Sound, so keep a collection of them somewhere or make them otherwise accessible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!