+2 votes
by (180 points)

Hello, I am a TWINE baby working in Harlowe 2.1.0 and trying really hard. 

I understand that Harlowe doesn't have audio marcos and therefore, I have managed to get my music in javascript playing on a continuous loop as my background music with: 

var audio= document.createElement('audio');
audio.src=
	'music.mp3';
audio.loop = true;
audio.play();

BUT, I also want to also add sound effects. My goal is to create an old style gameboy-like speech bubble with those "brrrrrrr" sound effects as the text passes across the screen (I'm doing this with the type writer effect). So, my question is: how do I get an audio to play at the start of every new passage while still keeping my audio on a continuous loop in the background.

pls help me. 

my crops are dying.

1 Answer

+1 vote
by (2.7k points)

Hi alebot,

you were quite near, I guess!

I simply copied your code into two passages ,  1 with other mp3 and without loop, see below.

I did not change the story javascript, just added to the passages.

So effect is:

  • Opening the story: music.mp3 sounds nicely.
  • Switching to Next passage: effect.mp3 'tata's immediately, and music still plays.

Start passage: (your code unchanged)

Check the tune
<script>
var audio= document.createElement('audio');
audio.src=
	'music.mp3';
audio.loop = true;
audio.play();
</script>

[[Next]]

 

The 'Next' passage contains the same code slightly changed:

Effect Sound bouncing in?

<script>
var audio= document.createElement('audio');
audio.src=
	'effect.mp3';
audio.loop = false;
audio.play();
</script>

And Tune still to hear?


[[Start]]

Does this work for you?

 

So if you want to play the same sound on really 'each new passage', then it might work without copying the script code to each passage:

Instead, create one extra passage with the <script>...</script> snippet inside and do add the tag header to the passage.

Then this script should play on any passage you switch to in this story.

*BUT* - as I see while trying that - this will make the effect.mp3 also play on the Start passage of the story,   To avoid that, the 'header' tagged passage should look like this (assuming the start passage of the Story is named 'InTheMorning')

Passage with any name but tagged with: header

(if: (passage:)'s name is "InTheMorning")[\
     (set: $playSound to false)]
(else:)[(set: $playSound to true)]

(if: $playSound)[
<script>
var audio= document.createElement('audio');
audio.src=
	'effect.mp3';
audio.loop = false;
audio.play();
</script>
]

Instead of the  (if: $playSound)[.....]  a simple $playSound[....] would be sufficient.

So, finally I hope, this answer still comes just in time for your story telling, as it was already posted 3 days ago.

Regards

by (2.7k points)

I'm not sure, whether I got your question right.

Perhaps you might have all that code stuff and do hear nothing anyway?

Then the wiki link
http://twinery.org/wiki/twine2:add_an_image_movie_sound_effect_or_music 
might help, which led me to that point, that I had to 'publish the story' to the same place on local disk, where the .mp3 files were located.

There was no sound when simply pressing  the 'Play' button from inside Twine2 (online or offline). That did work for showing the passage workflow, but did not play any sound.

I had to extra publish the story to local disk.

Greetings

...