0 votes
asked by (1.1k points)
Hello people!

Using SugarCube 2.21, how do you fade out audio and then stop it in the same passage?

I could stop it in a separate passage, but it would be far more convenient and save a lot of time to stop it in the same passage, and I may encounter a situation where I could not stop it in a separate passage.

Thank you!

1 Answer

0 votes
answered by (2.2k points)

I even made a whole widget for this. It looks like this:

<<widget "fadestop">>
  <!-- usage: <<fadestop "some_track">> or <<fadestop "some_track" 2>> -->
  <<set _fadeStopDuration = $args[1] || 3>>
  <<audio $args[0] fadeoverto _fadeStopDuration 0>>
  <<timed `_fadeStopDuration + 's'`>>
    <<audio $args[0] stop>>

The trick is to use <<timed>> macro to perform delayed action.

commented by (1.1k points)
Thank you; I was unable to get on earlier. Now, the macro library says timed functions are canceled when you go to another passage, so it would be a gamble whether it would work or not.

The main reason I wanted this at the moment was so that an audio track could be started at full volume, whereas if you had gone through a passage which had it fade out, it only continues to play silently. I wanted to combine either stop with fade out, or combine fade in with loop play. I found that I can actually do the latter, so I am good for now, but still would like to know the former for knowledge's sake.
commented by (2.2k points)

My bad, I haven't noticed that in docs.

No problem, we'll just do it more low-level way:

<<widget "fadestop">>
  <!-- usage: <<fadestop "some_track">> or <<fadestop "some_track" 2>> -->
  <<set _fadeStopDuration = $args[1] || 3>>
  <<audio $args[0] fadeoverto _fadeStopDuration 0>>
    setTimeout(function () { 
        $.wiki('<<audio $args[0] stop>>') 
      _fadeStopDuration * 1000

I haven't tested it, but the idea is to use javascript setTimeout, which works across passage transitions, and $.wiki which allows you to run any SugarCrube code from pure javascript.

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.