# How to control Fmod audio loop in code

**URL:** https://qa.fmod.com/t/how-to-control-fmod-audio-loop-in-code/18125
**Category:** Unity
**Tags:** unity
**Created:** [December 21, 2021, 9:54am UTC](https://qa.fmod.com/t/how-to-control-fmod-audio-loop-in-code/18125 "2021-12-21T09:54:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Tzqinglang](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@Tzqinglang](https://qa.fmod.com/u/Tzqinglang)
#### Post date: [December 21, 2021, 9:54am UTC](https://qa.fmod.com/t/how-to-control-fmod-audio-loop-in-code/18125/1 "2021-12-21T09:54:04Z")

</div>

How to control Fmod audio loop in code

---

<div class="post-metadata">

### Author: ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)
#### Post date: [December 23, 2021, 4:05am UTC](https://qa.fmod.com/t/how-to-control-fmod-audio-loop-in-code/18125/2 "2021-12-23T04:05:20Z")

</div>

[Loop Regions](https://fmod.com/resources/documentation-studio?version=2.02&page=glossary.html#loop-region) are created in FMOD Studio, along with parameter or event conditions that define when to leave the loop region. Parameters can then control the loop region using [`EventInstance::setParameterByName`](https://fmod.com/resources/documentation-api?version=2.02&page=studio-api-eventinstance.html#studio_eventinstance_setparameterbyname) if you have set it up to use parameter conditions, and if you have set it up to use event conditions then you just need to play the linked event to leave the loop.

I recommend you read through the [Timeline Logic](https://fmod.com/resources/documentation-studio?version=2.02&page=authoring-events.html#timeline-logic) section of the FMOD Studio documentation, and load the “Documents\FMOD Studio\examples\Examples.fspro” example project in FMOD Studio to have a look at how parameters are being used to change the loop region, especially in the “Music\>Level 01” and “Music\>Level 03” events.

Here is a basic example of how you would then control parameter values at runtime with the “Level 01” event to change the loop points:

```auto
using UnityEngine;
using FMODUnity;

public class LoopPoints : MonoBehaviour
{
    public EventReference Event = EventReference.Find("event:/Music/Level 01");
    
    public float ProgressionParameter = 0.0f;
    public float StingerParameter = 0.0f;

    private FMOD.Studio.EventInstance _eventInstance;

    private float lastParam = 0.0f;

    void Start()
    {
        _eventInstance = RuntimeManager.CreateInstance(Event);
        _eventInstance.start();
    }

    void Update()
    {
        if(ProgressionParameter + StingerParameter != lastParam)
        {
            _eventInstance.setParameterByName("Progression", ProgressionParameter);
            _eventInstance.setParameterByName("Stinger", StingerParameter);

            lastParam = ProgressionParameter + StingerParameter;
        }
    }
}

```
