Forum Discussion
Dynamically fading audio in SL
Hi,
I have created an interaction in SL which has background audio. The interaction isn't timed as it's dependant on the user's interaction. I want to be able to dynamically fade the audio out once the user has reached a certain point in the interaction.
I can achieve this by adding the audio using the Background audio function and manually adding triggers to adjust the volume down incrementally at certain points. This works well for one slide but I have multiple slides with different background audios on them so I am unable to manually select what audio from the playlist I would like to play on that particular slide.
Any suggestions other than cutting the audio completely on the other slides??
Thanks
Phil
4 Replies
- ThierryEMMANUELCommunity Member
Hello PhilEagles-db80 .
If I understand your request correctly, there is a very simple way to choose the audio you want by adding it to the slide with your interaction (either at the beginning or through a learner action) and using the settings in the background audio panel. See attached file, screens 1-2-3-4.
If you want to dynamically adjust the volume of the module through learner actions and/or the timeline, without touching the Player's adjustment button, you need to use JS code.
Three months ago, Nedim (thanks to him again!) (and again) proposed a good solution using a dynamic slider: Custom volume slider | Articulate - CommunityI modified the code (ChatGPT did it!) to keep only one variable. You can now change the volume dynamically whenever you want on each slide. Screen 5.
Let me know if this meets your request. File.story attached.- NedimCommunity Member
ThierryEMMANUEL Every time we come across the same post, something extraordinary comes out of it. Your work motivates me to go even further beyond.
I came up with a modified function that can fade the volume in or out as needed. It works with both background and slide-level audio, as long as only one audio file is playing at a time. Simulating this functionality with multiple audio files is possible, but it would require additional coding to target each audio source individually. This aligns with the original post topic, 'Dynamically fading audio in SL', but I'm not sure whether there are additional audio tracks on the slide that are meant to play simultaneously with the background audio—where only one fades, or vice versa.
However, this is the one where the background audio can be smoothly faded in or out. There is a global function that executes when the timeline starts, which includes both fade-in and fade-out volume functionality. This means you can call the fadeIn() or fadeOut() functions using an "Execute JavaScript" trigger at any point—whether it's after an interaction is completed, an object is clicked, the timeline reaches a certain point, or when a variable changes. You can even pass an argument to the function to control the fade duration — for example, fadeOut(5000) will trigger a 5-second fade out.
At the same time, I've connected and synchronized a slider to the DS volume, allowing its value to be used to trigger other events. In this simple example, the opacity of an image changes dynamically based on the slider’s value, moving back and forth. Of course, this technique can be applied to just about anything.
The slider is optional, the fade-in and fade-out functions work independently, but this example demonstrates how we can now connect and control other properties through volume or slider values.
I've attached the Story file for you to explore.- ThierryEMMANUELCommunity Member
It's Christmas in August. Thanks, Nedim . I do need more time to explore the different codes. I hope PhilEagles-db80 finds something useful in your answer or mine. If you need my motivation to make you work harder and longer, I'm here whenever you need me 😁.
- PhilEagles-db80Community Member
Thanks both, that's been great help! I have managed to get it working now :) One thing to be aware of that I didn't realise at the time is that this effects the global volume of the player. So for example if you wanted the background music to fade but voiceover to have it's own independent volume you need to create a separate element in JavaScript and add the voiceover file as a resource and point to that in the JavaScript.
Related Content
- 2 months ago
- 9 months ago