# Conditional Audio Playback

**URL:** <https://forums.ankiweb.net/t/conditional-audio-playback/54841>\
**Category:** Card Design\
**Created:** [January 25, 2025, 1:14am UTC](https://forums.ankiweb.net/t/conditional-audio-playback/54841 "2025-01-25T01:14:43Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![Anon\_0000](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@Anon\_0000](https://forums.ankiweb.net/u/Anon_0000)\
**Post date:** [January 29, 2025, 3:21am UTC](https://forums.ankiweb.net/t/conditional-audio-playback/54841/27 "2025-01-29T03:21:38Z")

</div>

Oddly enough, your `{{Back}}` field is the issue. It shows the following in HTML view:

```auto
acabar<br>
terminar

```

This breaks the code though, because I didn’t think multiline strings would be needed here. For reference, the following wouldn’t break the code:

```auto
acabar<br>terminar

```

I solved this issue. I also substituted the missing language files with test files to verify that audio playback works. It now works fine for me using your example deck.  
I only changed the `var count =…` line to multiline. But for reference, here is the entire front template:

```html
<div class="background-layer {{Tags}}"></div>
<div class="word-type">{{WType}}</div>
<hr>
<span class="word {{F_Classes}}">{{Front}}</span> {{F_Audio}}

<script>
	// count how many <br> there are. Also see: https://stackoverflow.com/a/4009768
	var count = (`{{Back}}`.match(/<br>/g) || []).length;
	
	// check if item "did_run" exists in sessionStorage. This is necessary to make
	// the variable persist. Also see: https://forums.ankiweb.net/t/is-it-even-possible-to-persist-javascript-variables-on-ankidroid/19820/2?u=anon_0000
	// if it doesn't exist, then the audio in the front never played.
	if (!sessionStorage.getItem("did_run")) {
		// if count is above 0, then there is a synonym
		if (count > 0) {
			var audio = new Audio('_ding.ogg');
			audio.play();
		}
		
		// now set the "did_run" in sessionStorage. This makes it so that
		// the audio won't be replayed again, if the backside is viewed.
		sessionStorage.setItem("did_run", JSON.stringify("yes"));
	} else {
		// The sessionStorage entry exists, which means the audio played before.
		// The audio might have played in a previous card though (e.g. if you
		// review card A and then B, B wouldn't play the audio anymore because
		// "did_run" is set. That's an issue). To solve this, we clear the
		// storage every time.
		sessionStorage.clear();
	}
</script>

```

Except for updating the front card, there is nothing more you have to do. It should work this time.

---

_[View the full topic](https://forums.ankiweb.net/t/conditional-audio-playback/54841)._
