# Unity how to play audio action on pressed button having an ending audio part

**URL:** <https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687>\
**Category:** Unity\
**Created:** [April 24, 2020, 1:56pm UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687 "2020-04-24T13:56:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![black\_racer](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@black\_racer](https://qa.fmod.com/u/black_racer)\
**Post date:** [April 24, 2020, 1:56pm UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/1 "2020-04-24T13:56:17Z")

</div>

Hi guys, i have a question, how make this sound action:

–\>button pressed

1. Start audio effect part
2. Middle audio effect part is looping (only in pressed button)

–\>button released

1. Play end audio effect

I could easily do the first two actions, but how to make the third audio part play is correctly.  
When I release the button, I need to break the loop and play the ending?

---

<div class="post-metadata">

**Author:** ![FoxyMusic](https://avatars.discourse-cdn.com/v4/letter/f/a4c791/32.png) [@FoxyMusic](https://qa.fmod.com/u/FoxyMusic)\
**Post date:** [April 24, 2020, 3:08pm UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/2 "2020-04-24T15:08:00Z")

</div>

You can do it using Key\_off functionality. Just place sustain point at the end of you loop ( it should be async and looped on item by the way, cut activated too) and your ending sound right after the sustain point. Your loop area should be short enough for cases when player will press and release button very quick.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/3/35b5efe90f76323e4a399ff7a3a132f5c9de9fa7.png)

---

<div class="post-metadata">

**Author:** ![black\_racer](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@black\_racer](https://qa.fmod.com/u/black_racer)\
**Post date:** [April 24, 2020, 5:36pm UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/3 "2020-04-24T17:36:52Z")

</div>

Thank you, but it doesn’t work quite correctly, I need the sound to break off and go to the final part, and the method you showed makes an event when the region is finished playing.  
That is, I need to break the loop at any time when I release the key and play final audio…

---

<div class="post-metadata">

**Author:** ![joseph](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/joseph/32/264_2.png) [@joseph](https://qa.fmod.com/u/joseph)\
**Post date:** [April 27, 2020, 2:00am UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/4 "2020-04-27T02:00:03Z")

</div>

I’m afraid I don’t understand what you mean by “the method you showed makes an event when the region is finished playing.” As far as I can tell, FoxyMusic’s method does not create new events or event instances at any point. Do you mean it triggers an instrument when the playback position leaves the loop region? If so, how does that differ from what you wanted?

Also, have you looked at the “Music/Level 03” event in the “examples.fspro” project that comes with FMOD Studio? It contains transition regions and loop regions designed to cause the playback position to jump from one part of an audio file to another when a parameter value changes. I believe these features could be used to get the behavior you want.

---

<div class="post-metadata">

**Author:** ![Paalo](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/paalo/32/616_2.png) [@Paalo](https://qa.fmod.com/u/Paalo)\
**Post date:** [May 24, 2020, 5:19am UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/5 "2020-05-24T05:19:05Z")

</div>

@black_racer (and also @joseph) I made an example project on how you can make an event which plays a OneShot as LeadIn → Loops → Plays another OneShot on LeadOut:

1. When Play() is called, it starts playing a LeadIn Sound + starts playing a loop
2. When Stop() is called, it transitions and plays a LeadOut Sound.

Here’s a Dropbox-link to the project if you want to try it out (it’s got a README and all 😉 )

> **[Loop\_PlayTailOnStop](https://www.dropbox.com/sh/il3qot48rqdlv1m/AAAbUzE0CLV-vQgA9FYqDST8a?dl=0)**
>
> Shared with Dropbox

_Here’s a video of it in action:_

[https://dl.dropboxusercontent.com/s/9scobs7dioq5rgi/Loop\_PlayTailOnStop%20-%20Video%20Showcase.mp4?dl=0](https://dl.dropboxusercontent.com/s/9scobs7dioq5rgi/Loop_PlayTailOnStop%20-%20Video%20Showcase.mp4?dl=0)

* * *

Here are some pics from the project:

_Main Event:_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/6/6565e612ad721bdb89788381da549c71de9370d3.png)

_Nested Event with Loop + Nested LeadIn Event:_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/f/f2bad1e9a58e32583f1327c36e64199f0b642c2c.png)

_Nested LeadIn Event:_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/c/ced7e63fc9cbc166c7dce6ffe9d5fd8836a8fe0f.png)

_Nested LeadOut (Tail) Event_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/f/f2138d6350064107a31c3223a81a34f61a3b4497.png)

_Parameter Value Modulation (the magic that makes it work)_  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/8/8662385d54b4fcb366871226fa2c3f24daf00020.png)

* * *

Here’s how it works:

**SUMMARY**  
The Main Event will play the tail/oneshot when the event is stopped, ie. avoid writing special code for that to happen, or playing the oneshot as a separate event.

**HOW IT WORKS**  
The “TailOnStop”-parameter is modulated through an AHDSR-modulator on the parameter-value-knob (in the parameter view).

Assuming the parameter’s initial value is “0”, this AHDSR-modulator will make the parameter set itself to “1” while the event is playing (ie. it is on)

When the event is stopped, the modulation will jump down the value to “0”, which triggers the transition to the “Tail”.

**FADE OUT MODULATIONS**  
The “loop”-sub event has an AHDSR-modulator to allow the loop to fade out (ie. crossfade with the Tail), if you so wish.

Additionally there’s an AHDSR-modulator on the Master-track of the main event, which makes the event play out the tail (make it audible) when it is stopped.

---

<div class="post-metadata">

**Author:** ![FoxyMusic](https://avatars.discourse-cdn.com/v4/letter/f/a4c791/32.png) [@FoxyMusic](https://qa.fmod.com/u/FoxyMusic)\
**Post date:** [May 25, 2020, 8:50am UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/6 "2020-05-25T08:50:41Z")

</div>

You just made it too comlecated. It works just the same way in one event. With all the same functionality. you can adjust fade time as you like, and even stop it as it made in your event with parameter on stop and transition region. But there’s defenetly no need for bunch of sub events. Just use async and loop functionality on your files directly.

[https://dl.dropboxusercontent.com/s/fi079mf2b6lghop/Fmod%20in\_loop\_end.mp4?dl=0](https://dl.dropboxusercontent.com/s/fi079mf2b6lghop/Fmod%20in_loop_end.mp4?dl=0)

---

<div class="post-metadata">

**Author:** ![Tomas\_F](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@Tomas\_F](https://qa.fmod.com/u/Tomas_F)\
**Post date:** [May 27, 2024, 5:20am UTC](https://qa.fmod.com/t/unity-how-to-play-audio-action-on-pressed-button-having-an-ending-audio-part/15687/7 "2024-05-27T05:20:23Z")

</div>

I would love any of you guys to explain this to me again, but as if I was the 5-years old child. I´ve been tring to make “on-stop” event for two days and I am dead. Nothing from these tutorials work for me.

EDIT: I figured where was my problem. When I set everything up (ADHSR envelope on parameter skipping to end), nothing was happening - everything was stopping immediatelly. BUT - I tried setting AHDSR modulator on the master volume of the whole event and that was what was causing the problem. I set AHDSR envelope for parameter “to tail” a little shorter than master volume envelope so that it had an actual time to go to the end and that solved it. If anyone is reading this post in the future - maybe this will help : )
